What is RIGHT JOIN?
JOIN Types Comparison
| JOIN Type | Shows |
|---|---|
| INNER JOIN | Only matches |
| LEFT JOIN | All from left table |
| RIGHT JOIN | All from right table |
| FULL OUTER JOIN | All from both tables |
4 rows
RIGHT JOIN shows ALL rows from the right table, even if there is no match in the left table.
Simple analogy: Opposite of LEFT JOIN. Shows all classes and which students are enrolled (even empty classes).
Basic Example
JOIN Types Comparison
| JOIN Type | Shows |
|---|---|
| INNER JOIN | Only matches |
| LEFT JOIN | All from left table |
| RIGHT JOIN | All from right table |
| FULL OUTER JOIN | All from both tables |
4 rows
SELECT students.name, enrollments.course
FROM students
RIGHT JOIN enrollments ON students.id = enrollments.student_id;Shows all enrollments, even if student does not exist.
When to Use
JOIN Types Comparison
| JOIN Type | Shows |
|---|---|
| INNER JOIN | Only matches |
| LEFT JOIN | All from left table |
| RIGHT JOIN | All from right table |
| FULL OUTER JOIN | All from both tables |
4 rows
Rarely used! LEFT JOIN is more common.
Most people rewrite RIGHT JOIN as LEFT JOIN by switching table order.
What Comes Next
JOIN Types Comparison
| JOIN Type | Shows |
|---|---|
| INNER JOIN | Only matches |
| LEFT JOIN | All from left table |
| RIGHT JOIN | All from right table |
| FULL OUTER JOIN | All from both tables |
4 rows
Next: FULL OUTER JOIN (shows all records from both tables)