MySQL RIGHT JOIN
MySQL RIGHT JOIN: Preserving Every Right Table Row
The MySQL RIGHT JOIN (or RIGHT OUTER JOIN) returns every row from the right table and the matching rows from the left table. When a row in the right table has no matching key in the left table, MySQL fills all left-table columns with NULL.
Basic Syntax
SELECT l.column1, r.column2
FROM left_table AS l
RIGHT JOIN right_table AS r
ON l.right_id = r.id;
Practical SQL Example
This query lists every department in demo_departments alongside assigned employees from demo_employees, keeping departments that currently have zero staff:
SELECT
d.id AS department_id,
d.department_name,
e.full_name AS employee_name
FROM demo_employees AS e
RIGHT JOIN demo_departments AS d
ON e.department_id = d.id
ORDER BY d.department_name ASC;
| department_id | department_name | employee_name |
|---|---|---|
| 1 | Engineering | Maya Patel |
| 2 | Marketing | Liam Chen |
| 3 | Research | NULL |
Common Mistakes and Best Practices
- Prefer
LEFT JOINOverRIGHT JOINin Team Codebases: EveryA RIGHT JOIN Bcan be rewritten cleanly asB LEFT JOIN A. Most SQL style guides standardize onLEFT JOINonly, because reading primary tables from top to bottom prevents confusion in 4- or 5-table joins. - Avoid Filtering Left-Table Columns in
WHERE: Just likeLEFT JOIN, puttingWHERE e.is_active = 1on aRIGHT JOINdrops unmatched right rows wheree.is_activeisNULL; move left-table filters into theONclause.
Frequently Asked Questions
Is there any performance difference between LEFT JOIN and RIGHT JOIN?
No, MySQL's optimizer converts RIGHT JOIN internally to an equivalent LEFT JOIN before generating the execution plan.
When would I ever see RIGHT JOIN in practice?
You mainly encounter it in legacy reports or SQL migrations where someone added an optional parent table at the end of a long query.
Are RIGHT JOIN and RIGHT OUTER JOIN identical?
Yes, OUTER is purely optional syntax and changes nothing about how MySQL runs the query.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.