Joins
LEFT JOIN
LEFT JOIN keeps every row from the left table, even with no match. Missing values become NULL.
SELECT s.name, COUNT(e.course_id) AS courses
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
GROUP BY s.name;Output
name | courses ------+-------- Ali | 1 Sara | 2 Bilal | 0
Bilal has no enrolments, but still appears thanks to LEFT JOIN.