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.
💻 Live Code Editor
Is page ki language (MYSQL) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.