00:00

MySQL RIGHT JOIN

Last updated: 3 min read

RIGHT JOIN is the mirror image of LEFT JOIN: it returns every row from the right table, plus matching rows from the left. Unmatched left-hand columns are NULL. Any RIGHT JOIN can be written as a LEFT JOIN by swapping the tables, which is why many teams use only LEFT JOIN.

1. What RIGHT JOIN Returns

RIGHT JOIN returns the whole right circleTwo overlapping circles, students on the left and departments on the right. The shaded area shows which rows the join returns.studentsdepartmentsstudents RIGHT JOIN departments
Figure 1: all departments are returned; a department without students gets NULLs in the student columns.
syntax.sql
SELECT columns
FROM left_table
RIGHT [OUTER] JOIN right_table ON left_table.col = right_table.col;

2. A Basic RIGHT JOIN

query.sql
SELECT s.name, d.dept_name
FROM students s
RIGHT JOIN departments d ON d.dept_id = s.dept_id
ORDER BY d.dept_name, s.name;

Result: 5 rows

namedept_name
AditiComputer Science
BilalComputer Science
NULLHistory
ChenPhysics
EshanPhysics

History has no students, so it appears once with name NULL. Diya, who has no department, is not shown, because she is on the left side.

3. Rewriting as a LEFT JOIN

Swap the table order and the result is identical:

query.sql
SELECT s.name, d.dept_name
FROM departments d
LEFT JOIN students s ON s.dept_id = d.dept_id
ORDER BY d.dept_name, s.name;

Result: 5 rows

namedept_name
AditiComputer Science
BilalComputer Science
NULLHistory
ChenPhysics
EshanPhysics
Reading left to right, "start from departments and add students" is easier to follow than a RIGHT JOIN. Prefer LEFT JOIN in new code; understand RIGHT JOIN when you meet it.

4. Finding Unmatched Right Rows

query.sql
SELECT c.title AS course_without_students
FROM enrolments e
RIGHT JOIN courses c ON c.course_id = e.course_id
WHERE e.course_id IS NULL;

Result: 1 row

course_without_students
World History

5. Counting per Right-Side Row

counts.sql
SELECT d.dept_name, COUNT(s.student_id) AS students
FROM students s
RIGHT JOIN departments d ON d.dept_id = s.dept_id
GROUP BY d.dept_id, d.dept_name
ORDER BY students DESC;

Result: 3 rows

dept_namestudents
Computer Science2
Physics2
History0

6. Building a FULL OUTER JOIN

MySQL has no FULL OUTER JOIN. Combine LEFT and RIGHT joins with UNION to keep unmatched rows from both sides:

full-outer.sql
SELECT s.name, d.dept_name FROM students s LEFT  JOIN departments d ON d.dept_id = s.dept_id
UNION
SELECT s.name, d.dept_name FROM students s RIGHT JOIN departments d ON d.dept_id = s.dept_id
ORDER BY dept_name IS NULL, dept_name, name;

Result: 6 rows

namedept_name
AditiComputer Science
BilalComputer Science
NULLHistory
ChenPhysics
EshanPhysics
DiyaNULL

Both Diya (no department) and History (no students) appear.

7. LEFT vs RIGHT vs INNER

JoinKeeps unmatched rows fromDiya (no dept)History (no students)
INNER JOINneither tabledroppeddropped
LEFT JOIN (students first)studentskeptdropped
RIGHT JOIN (students first)departmentsdroppedkept
LEFT UNION RIGHTbothkeptkept

8. Summary

  • RIGHT JOIN keeps every row of the right table; unmatched left columns are NULL.
  • A RIGHT JOIN B = B LEFT JOIN A; most code uses LEFT JOIN.
  • WHERE left.key IS NULL finds right rows without a match.
  • LEFT JOIN UNION RIGHT JOIN emulates FULL OUTER JOIN.