Labs ICT
โญ Pro Login

LEFT JOIN

LEFT JOIN is like INNER JOIN but with a twist โ€” it keeps all rows from the left table even if there is no match in the right table. For non-matching rows, the right table columns get NULL values.

LEFT JOIN Example

Let us add a new student who is not enrolled in any course:

INSERT INTO students VALUES (7, 'Halima', 19, 'Sokoto', 0);

-- Now see the difference:
SELECT s.name, c.course_name
FROM students s
LEFT JOIN enrollments e ON s.id = e.student_id
LEFT JOIN courses c ON e.course_id = c.id;

Compare this with INNER JOIN. Halima shows up even though she is not enrolled, with NULL for course_name. INNER JOIN would have dropped her completely.

Finding Unmatched Rows

This is a super common use case โ€” find rows in one table that have no match in another:

-- Students who are not enrolled in any course
SELECT s.name
FROM students s
LEFT JOIN enrollments e ON s.id = e.student_id
WHERE e.course_id IS NULL;

This pattern is extremely useful. "Find customers who never ordered," "Find products that were never sold," "Find employees who never took a leave" โ€” all use the same LEFT JOIN + IS NULL trick.

๐Ÿงช Quick Quiz

What does LEFT JOIN return?