RIGHT JOIN & When to Rewrite as LEFT JOIN
Understand RIGHT JOIN and why teams usually rewrite it as LEFT JOIN.
In plain words
RIGHT JOIN keeps everything from the second table. Swap the two tables and say LEFT JOIN instead, and you get the same result.
Think of it like…
Reading a sentence backwards still gives the same meaning, it's just harder to follow. RIGHT JOIN is a LEFT JOIN written from the other end.
Where you’ve seen it
SELECT p.name, oi.order_id FROM order_items oi RIGHT JOIN products p ON p.id = oi.product_id; lists every product, including never-ordered ones. The clearer version is FROM products p LEFT JOIN order_items oi ON oi.product_id = p.id.
Words you’ll hear
- Preserved side
- The right table in a RIGHT JOIN; all its rows are kept.
- Equivalence
- A RIGHT JOIN B = B LEFT JOIN A.
- Readability
- Teams standardise on LEFT JOIN so the kept table is always first.
- Mixed joins
- Combining LEFT and RIGHT joins in one query is hard to reason about.
Visual diagram
Theory & example queries
What RIGHT JOIN returns
For each row of the right table, the database finds left rows that satisfy ON. Matches are combined; right rows with no match appear once with NULLs in the left table's columns. Left rows with no match are dropped.
All the LEFT JOIN rules apply in mirror image. Conditions on the left (optional) table belong in ON, because in WHERE they'd remove the unmatched right rows. COUNT(left.id) gives 0 for unmatched rows, while COUNT(*) gives 1.
| id | order_id | product_id | qty | price |
|---|---|---|---|---|
| 1 | 101 | 42 | 2 | 49.5 |
| 2 | 101 | 84 | 1 | 150 |
| 3 | 102 | 42 | 1 | 49.5 |
| id | name | price | category | stock |
|---|---|---|---|---|
| 42 | SQL Notebook | 49.5 | books | 18 |
| 84 | Mechanical Keyboard | 150 | hardware | 5 |
| 11 | Query Mug | 12.99 | gifts | 33 |
SELECT p.id, COUNT(oi.order_id)FROM order_items oi RIGHT JOIN products p ON p.id = oi.product_idGROUP BY p.id;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative product counts3 sample rows
| id | count |
|---|---|
| 41 | 3 |
| 42 | 0 |
| 56 | 1 |
Note: Sample data assumes product 42 has never been ordered. RIGHT JOIN keeps it and COUNT(oi.order_id) returns zero because the optional order columns are NULL.
Key takeaway: RIGHT JOIN keeps the right table; everything else mirrors LEFT JOIN.
Why teams rewrite it as LEFT JOIN
SQL reads left to right, and most people reason about joins as "start with this table and attach optional data". Putting the preserved table first with a LEFT JOIN matches that mental model, so many style guides simply forbid RIGHT JOIN.
Mixing LEFT and RIGHT joins in one query makes it hard to tell which tables are preserved at each step. If a query needs every row of a table that appears later in a long chain, reorder the joins so that table comes first, or wrap parts in a subquery. RIGHT JOIN is still occasionally handy when appending one optional table to an already long query, but the rewrite is always possible.
FROM order_items oi RIGHT JOIN products p ON p.id = oi.product_id → FROM products p LEFT JOIN order_items oi ON oi.product_id = p.id. Same rows, easier to read.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Find the preserved table
In a RIGHT JOIN, the table after the JOIN keyword is the one whose rows are all kept.
…RIGHT JOIN products p …When to use it & trade-offs
What it is
RIGHT JOIN keeps every row from the table on the *right* of the join, plus matching rows from the left, with NULLs where the left side has no match. It's exactly a LEFT JOIN with the tables swapped: A RIGHT JOIN B ON … returns the same rows as B LEFT JOIN A ON ….
Why it matters: Most teams rarely write RIGHT JOIN, because queries are easier to read when the table you're keeping comes first. But you'll meet it in existing code and in "types of joins" interview questions, so you need to read it confidently and know how to rewrite it.
When to reach for it
- Understanding and refactoring older SQL
- Answering "types of joins" questions
- Very occasionally, appending a preserved table at the end of a query
When to avoid or be careful
- Mixing LEFT and RIGHT joins in one query
- Using RIGHT JOIN where a LEFT JOIN reads more naturally
- Filtering the optional (left) table in WHERE
RIGHT JOIN vs LEFT JOIN vs FULL OUTER JOIN
Strengths: Keeps all right-table rows without reordering.
Watch out: Harder to read; confusing when mixed with LEFT JOINs.
Strengths: Preserved table first; easy to follow.
Watch out: May require reordering the FROM clause.
Strengths: Reconciliation in both directions.
Watch out: NULLs on either side; not in MySQL.
Interview signals & numbers to know
When this topic comes up
- “What's the difference between LEFT and RIGHT JOIN?”
- Legacy queries with RIGHT JOIN
- SQL style guides
- Types of joins
- Refactoring for readability
Benchmarks & rules of thumb
- A RIGHT JOIN B
- = B LEFT JOIN A
- All right rows
- kept
- SQLite 3.39
- first version with RIGHT and FULL JOIN
Common mistakes & follow-ups
Common interview mistakes
- Rewriting one RIGHT JOIN in the middle of a long chain without reordering the other joins, which changes which rows are preserved.
- Adding a WHERE filter on the left table's columns and losing the unmatched right rows.
- Combining LEFT and RIGHT joins and losing track of which rows are preserved.
Likely interviewer follow-ups
- Rewrite this RIGHT JOIN as a LEFT JOIN.
- Is there anything a RIGHT JOIN can do that a LEFT JOIN can't?
- Why do many style guides discourage RIGHT JOIN?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply RIGHT JOIN & When to Rewrite as LEFT JOIN with hands-on SQL practice and interview Q&A.