⬅ Previous Topic
SQL INNER JOINNext Topic ⮕
SQL RIGHT JOIN⬅ Previous Topic
SQL INNER JOINNext Topic ⮕
SQL RIGHT JOINLet’s say your school database has a list of students and a separate table with their exam results. But not every student has appeared for the exam yet. If you want to list all students — whether they have marks or not — LEFT JOIN
is what you need.
LEFT JOIN
returns all records from the left (first) table, and the matched records from the right (second) table. If there’s no match, NULL values are returned for the right table’s columns.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A'),
(2, 'Diya Iyer', '9B'),
(3, 'Sneha Patil', '10A'),
(4, 'Mehul Agarwal', '8C'),
(5, 'Saira Bano', '9B');
CREATE TABLE results (
roll_no INT,
subject VARCHAR(30),
marks INT
);
INSERT INTO results VALUES
(1, 'Maths', 85),
(2, 'Maths', 92),
(3, 'Maths', 78);
Let’s list all students, including those without results:
SELECT s.name, s.class, r.subject, r.marks
FROM students s
LEFT JOIN results r
ON s.roll_no = r.roll_no;
name | class | subject | marks
----------------+--------+---------+-------
Aarav Sharma | 10A | Maths | 85
Diya Iyer | 9B | Maths | 92
Sneha Patil | 10A | Maths | 78
Mehul Agarwal | 8C | NULL | NULL
Saira Bano | 9B | NULL | NULL
Explanation: Mehul and Saira have no results yet, so subject
and marks
are shown as NULL
.
If you want only students who don’t have results:
SELECT s.name, s.class
FROM students s
LEFT JOIN results r ON s.roll_no = r.roll_no
WHERE r.roll_no IS NULL;
name | class
----------------+-------
Mehul Agarwal | 8C
Saira Bano | 9B
Let’s add a second subject for student 1 and see how LEFT JOIN handles it:
INSERT INTO results VALUES (1, 'Science', 90);
SELECT s.name, r.subject, r.marks
FROM students s
LEFT JOIN results r ON s.roll_no = r.roll_no
ORDER BY s.name, r.subject;
name | subject | marks
----------------+---------+-------
Aarav Sharma | Maths | 85
Aarav Sharma | Science | 90
Diya Iyer | Maths | 92
Sneha Patil | Maths | 78
Mehul Agarwal | NULL | NULL
Saira Bano | NULL | NULL
If you’re generating a report for the school principal showing all students — even those who missed exams — LEFT JOIN
gives you a complete picture without losing anyone.
The LEFT JOIN
clause ensures no one is left behind — even if there’s no match in the right table. Whether you're analyzing student participation, attendance, or grades, LEFT JOIN helps paint the full picture by prioritizing your left-side data.
Next, we’ll look at RIGHT JOIN — which flips the logic, keeping all rows from the right table instead.
SELECT s.name, c.class_name FROM students s LEFT JOIN classes c ON s.class_id = c.class_id;
⬅ Previous Topic
SQL INNER JOINNext Topic ⮕
SQL RIGHT JOINYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.