LEFT JOIN
LEFT JOIN keeps all rows of the left table. Where there is no match on the right, its columns are NULL. It is the classic way to find rows that have no related rows, such as students with no enrolment.
Example
SELECT students.name, enrolments.course_id
FROM students
LEFT JOIN enrolments ON enrolments.student_id = students.id
WHERE students.id IN (4, 5);Result
name | course_id Wanjiru | NULL Tunde | 1
Example
SELECT students.name
FROM students
LEFT JOIN enrolments ON enrolments.student_id = students.id
WHERE enrolments.student_id IS NULL
ORDER BY students.name;Result
name Esi Sipho Wanjiru
Exercise
Keep all rows from the left table.
SELECT * FROM a JOIN b ON a.id = b.a_id;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.