RIGHT JOIN
RIGHT JOIN is the mirror image of LEFT JOIN: it keeps all rows of the right table. Most people swap the table order and use LEFT JOIN instead, which reads more naturally.
Example
SELECT courses.title, enrolments.student_id
FROM enrolments
RIGHT JOIN courses ON courses.id = enrolments.course_id
WHERE courses.id = 5 OR courses.id = 3
ORDER BY courses.id;Result
title | student_id JavaScript | 1 SQL | 3
- Older SQLite versions (before 3.39) do not support RIGHT JOIN. MySQL, PostgreSQL and SQL Server do.
Exercise
Keep all rows from the right 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.