The RIGHT OUTER JOIN (or RIGHT JOIN) is the symmetrical mirror of the LEFT JOIN. It returns all rows from the right table (Table B), along with matching rows from the left table (Table A). If no match exists in the left table, NULL values are returned for Table A's columns.
-- RIGHT JOIN preserves all departments, even Arts (dept_id 30) with 0 students
SELECT
d.dept_id,
d.dept_name,
s.student_id,
s.first_name
FROM students s
RIGHT JOIN departments d ON s.dept_id = d.dept_id;
| dept_id | dept_name | student_id | first_name |
|---|---|---|---|
| 10 | Engineering | 1 | Aman |
| 10 | Engineering | 3 | Rohan |
| 20 | Business | 2 | Pooja |
| 30 | Arts | NULL | NULL |
In professional engineering teams, coding standards prefer LEFT JOIN for readability. To convert any RIGHT JOIN, simply swap the order of the tables:
-- Identical query written cleanly as a LEFT JOIN:
SELECT
d.dept_id,
d.dept_name,
s.student_id,
s.first_name
FROM departments d
LEFT JOIN students s ON d.dept_id = s.dept_id;
Next, let us learn how to retain all rows from both tables simultaneously using SQL FULL OUTER JOIN!
LEFT JOIN preserves all rows from the first (left) table. RIGHT JOIN preserves all rows from the second (right) table.
Humans read from left to right. Sticking exclusively to LEFT JOIN makes queries consistent and easier to maintain. Any RIGHT JOIN can be converted to an identical LEFT JOIN simply by swapping the table order in the FROM and JOIN clauses.
Yes! Running 'students RIGHT JOIN departments' preserves all departments, and any department with no students will have NULLs in the student columns.
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 37: FULL OUTER JOIN → Previous: ← LEFT JOIN | Full Syllabus
Why senior developers rewrite RIGHT JOINs as cleaner LEFT JOINs.
Filtering strictly for mutually matching rows.
Preserving unmatched records from both tables.
Visualizing relational joins with Venn diagrams.