SQL: difference between INNER JOIN, LEFT JOIN and RIGHT JOIN?
asked 5 months ago307 viewsen
I have students and enrollments. Some students have no enrollment. Which JOIN shows all students, including those without enrollments?
AI diagnosis
AI-generated. Not an answer — the community below confirms or corrects it. Always verify before relying on it.
2 Answers
- INNER JOIN: only rows that match in both tables. Students without enrollments disappear. - LEFT JOIN: all rows from the left table, plus matches from the right. Where there's no match, the right-side columns are NULL. - RIGHT JOIN: the mirror of LEFT. Rarely used, since you can just swap the table order.
SELECT s.name, e.course_id
FROM students s
LEFT JOIN enrollments e ON e.student_id = s.id;
To find students with no enrollment:
SELECT s.*
FROM students s
LEFT JOIN enrollments e ON e.student_id = s.id
WHERE e.student_id IS NULL;
Careful: a condition on the right table in WHERE (e.g. WHERE e.year = 2026) turns your LEFT JOIN back into an INNER JOIN. Put it in the ON clause instead.
Sign in to tell others whether this worked.
A visual trick that helps: draw both tables as circles. INNER = the overlap only; LEFT = the whole left circle plus the overlap. Keep in mind the analogy breaks down with duplicates: a LEFT JOIN can return *more* rows than the left table if one student has several enrollments.
Sign in to tell others whether this worked.
Sign in and verify your email to post an answer.