RIGHT JOIN
Overview
A RIGHT JOIN (or RIGHT OUTER JOIN) returns all records from the right table, along with matching records from the left table.
Learning Objectives
- Understand
RIGHT JOINsemantics. - Compare
RIGHT JOINvsLEFT JOINquery readability. - Re-order tables to prefer
LEFT JOINfor consistency.
Detailed Concept Explanation
RIGHT JOIN is the exact inverse of LEFT JOIN. It preserves all rows from the right table regardless of whether a matching record exists in the left table.
In practice, most SQL developers avoid RIGHT JOIN and simply swap the order of tables inside a LEFT JOIN to make queries easier to read from left to right.
Code Examples
SQL
-- RIGHT JOIN example
SELECT
e.name AS employee_name,
d.dept_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;
-- Equivalent LEFT JOIN (Preferred for readability)
SELECT
e.name AS employee_name,
d.dept_name
FROM departments d
LEFT JOIN employees e ON d.department_id = e.department_id;
Best Practices
- Prefer LEFT JOIN: Standardize code bases on
LEFT JOINby placing driving tables first. This improves query scannability.
Interview Perspective
Note
Interview Question: Can any RIGHT JOIN be converted into a LEFT JOIN?
Yes. Simply swap the order of the two tables in the FROM and JOIN clauses. A RIGHT JOIN B is logically identical to B LEFT JOIN A.
Interactive Challenges
Summary
RIGHT JOIN preserves all rows from the right table. Reordering tables to use LEFT JOIN is preferred for readability.
DataPlayArena Technical Editorial Board
Curated & maintained by enterprise data platform engineers with 10+ years in distributed systems.
Runtime Verification Standards: Every code example, query, and architecture diagram in this tutorial is compiled and verified against real runtimes (Apache Spark 3.5, Python 3.11, PostgreSQL, and DuckDB). If you spot any errata or want to suggest improvements, please submit feedback.