MySQL Right Join
RIGHT JOIN keeps every row from the right table, filling in NULLs for any left-table columns that don't have a match - the mirror image of LEFT JOIN.
RIGHT JOIN as the Mirror of LEFT JOIN
RIGHT JOIN keeps every row of the table listed after the keyword, regardless of whether it matches anything in the table on the left. Wherever there's no match, the left table's columns come back as NULL instead of the row being dropped. It behaves exactly like LEFT JOIN with the two tables swapped, but the FROM clause keeps its original table order.
Every department, with employees if assigned
SELECT e.name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;Why RIGHT JOIN Is Rarely Used in Practice
Because RIGHT JOIN and LEFT JOIN describe the same shape from opposite ends, most style guides prefer writing every join as a LEFT JOIN and simply reordering the FROM clause, since it reads more naturally left-to-right and keeps a codebase consistent. The query above can be rewritten with departments listed first and LEFT JOIN used instead, producing an identical result.
The same result written as a LEFT JOIN
SELECT e.name, d.department_name
FROM departments d
LEFT JOIN employees e ON e.department_id = d.department_id;Finding Unmatched Rows on the Right
The same anti-join pattern used with LEFT JOIN works with RIGHT JOIN, just aimed at the other table: filter on the left table's key being NULL to isolate rows on the right that have no match at all.
Departments with no employees assigned
SELECT d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id
WHERE e.employee_id IS NULL;- RIGHT JOIN keeps every row of the table on the right side of the keyword, regardless of a match.
- table_a RIGHT JOIN table_b ON ... always produces the same result as table_b LEFT JOIN table_a ON ....
- MySQL doesn't support FULL OUTER JOIN directly; combining a LEFT JOIN and a RIGHT JOIN with UNION is the usual workaround for keeping unmatched rows from both sides.
- If a codebase standardizes on LEFT JOIN only, seeing a RIGHT JOIN in a diff is often worth double-checking.