Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 46/ 160
03. Joins & Relational Querying

RIGHT JOIN & When to Rewrite as LEFT JOIN

Understand RIGHT JOIN and why teams usually rewrite it as LEFT JOIN.

BeginnerRIGHT JOIN · Rewrite as LEFT3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Products with their order countsQuery example
1Input dataStart from the source rows or state that the statement sees.
order_items
3 rows
order_items sample rows
idorder_idproduct_idqtyprice
110142249.5
2101841150
310242149.5
products
3 rows
products sample rows
idnamepricecategorystock
42SQL Notebook49.5books18
84Mechanical Keyboard150hardware5
11Query Mug12.99gifts33
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1SELECT2  p.id,3  COUNT(oi.order_id)4FROM5  order_items oi6  RIGHT JOIN products p ON p.id = oi.product_id7GROUP BY8  p.id;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Project and calculate

Calculate the selected expressions and return the requested output columns.

1/8
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative product counts3 sample rows
Representative product counts
idcount
413
420
561

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.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

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.

The rewrite

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.

Key takeaway: Prefer LEFT JOIN with the kept table first; avoid mixing directions.
Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Reading

Find the preserved table

In a RIGHT JOIN, the table after the JOIN keyword is the one whose rows are all kept.

SQL at this stepReadable preview
1…2RIGHT JOIN products p …
What to watch at this step
Kept
right table
Tip: use ← → to move between steps

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

RIGHT JOIN
Best for: Reading existing code; rare cases at the end of a long join chain

Strengths: Keeps all right-table rows without reordering.

Watch out: Harder to read; confusing when mixed with LEFT JOINs.

LEFT JOIN (tables swapped)
Best for: The same result, written conventionally

Strengths: Preserved table first; easy to follow.

Watch out: May require reordering the FROM clause.

FULL OUTER JOIN
Best for: Keeping unmatched rows from both sides

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?
Interview tip: Say "A RIGHT JOIN B is B LEFT JOIN A", rewrite the example on the spot, and mention that you'd follow the team's style guide. Short, correct and practical.

Knowledge check

Loading quiz

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.

Open SQL Practice →SQL interview Q&A
Previous topic
Finding Missing Records (LEFT JOIN ... WHERE right.id IS NULL)
Next topic
FULL OUTER JOIN & Data Reconciliation Queries