MySQL + SQL · Lesson 68
LEFT JOIN and RIGHT JOIN
LEFT JOIN
LEFT JOIN returns ALL rows from the left table, plus matching rows from the right. Where there is no match, right columns are NULL.
SELECT s.name, c.class_name
FROM students s
LEFT JOIN classes c ON s.class_id = c.class_id;Every student appears; those without a class show NULL for class_name.
RIGHT JOIN
RIGHT JOIN is the mirror: ALL rows from the right table, plus matches from the left.
SELECT s.name, c.class_name
FROM students s
RIGHT JOIN classes c ON s.class_id = c.class_id;Every class appears, even classes with no students.
Summary
- LEFT JOIN keeps all left-table rows; RIGHT JOIN keeps all right-table rows.
- Missing matches become NULL.