LEFT JOIN (Left Outer Join) & Preserving Unmatched Rows
Keep every left-side row and fill missing right-side columns with NULL.
In plain words
Keep everything from the first table, and attach matches from the second table when they exist. Leave blanks when they don't.
Think of it like…
A class attendance sheet: every student on the roster is listed (the left table), and the "signed in at" column is filled only for students who showed up. Absent students still appear, with a blank time.
Where you’ve seen it
SELECT c.name, COUNT(o.id) FROM customers c LEFT JOIN orders o ON o.customer_id = c.id GROUP BY c.name lists customers with 0 orders. With an INNER JOIN they would vanish from the report.
Words you’ll hear
- Left table
- The table written before LEFT JOIN. All of its rows are kept.
- Unmatched row
- A left row with no right match; its right-side columns are NULL.
- Anti-join
- LEFT JOIN … WHERE right.id IS NULL returns the rows that have no match.
- Fan-out
- One left row matching many right rows produces several output rows.
Visual diagram
Theory & example queries
How LEFT JOIN builds its result
For each left row, the database finds right rows that satisfy the ON condition. One match gives one output row, three matches give three rows, and no match gives one row with NULL in every right-side column.
That makes counting subtle: COUNT(*) counts the NULL-padded row as 1, while COUNT(o.id) counts only real matches. Use COUNT(right_table.primary_key) for "number of related rows".
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT c.id, COUNT(o.id) AS ordersFROM customers c LEFT JOIN orders o ON o.customer_id = c.idGROUP BY c.id;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative customer order counts3 sample rows
| id | orders |
|---|---|
| 7 | 4 |
| 12 | 1 |
| 23 | 0 |
Note: Illustrative customers. COUNT(o.id), unlike COUNT(*), counts the right-side id and therefore returns zero for customer 23's NULL-padded unmatched row.
Key takeaway: Count the right table's key, not *.
The ON vs WHERE trap
A condition on the right table placed in WHERE runs after the join, and the NULL-padded rows fail it (NULL = 'paid' is UNKNOWN). The unmatched left rows disappear and the query behaves like an INNER JOIN.
Put right-table filters in the ON clause: they limit which rows can match while every left row is kept. Left-table filters stay in WHERE.
… LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid' keeps customers with no paid orders; WHERE o.status = 'paid' would drop them.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Choose the table you must keep
Put the table whose every row must appear first. That's usually the entity table, such as customers or products.
FROM customers cWhen to use it & trade-offs
What it is
LEFT JOIN returns every row from the left table plus the matching rows from the right table. When a left row has no match, the right table's columns come back as NULL. An INNER JOIN would drop those unmatched rows entirely.
Why it matters: Most real reports need "all customers, with their orders if any", and an INNER JOIN silently drops customers with zero orders. LEFT JOIN also powers anti-joins (rows with no match), and it's where the classic ON-vs-WHERE bug lives.
When to reach for it
- Reports that must include zero values, such as customers with no orders
- Optional relationships, such as users who may not have a profile yet
- Finding missing records with WHERE right.id IS NULL
When to avoid or be careful
- Filtering right-table columns in WHERE when you meant to keep every left row
- LEFT JOIN when every row is guaranteed to match; INNER JOIN states the intent better
- Chaining LEFT JOINs to several one-to-many tables, which multiplies rows
LEFT JOIN vs INNER JOIN vs NOT EXISTS
Strengths: Simpler results with no NULL handling.
Watch out: Silently drops unmatched rows.
Strengths: Keeps zero-match entities in reports.
Watch out: Needs NULL handling, and fans out when the right side has many matches.
Strengths: Clear intent, NULL-safe, and usually planned as an efficient anti-join.
Watch out: Returns no columns from the right table.
Interview signals & numbers to know
When this topic comes up
- “Include customers with zero orders”
- “Find users who never logged in”
- LEFT JOIN vs INNER JOIN
- Counts look too high after a join
- ON vs WHERE in outer joins
Benchmarks & rules of thumb
- ≥ left rows
- minimum result size
- NULL
- right-side columns when unmatched
- COUNT(o.id)
- 0 for unmatched rows; COUNT(*) gives 1
Common mistakes & follow-ups
Common interview mistakes
- Adding
WHERE o.status = 'paid'and accidentally turning the LEFT JOIN into an INNER JOIN. - Using COUNT(*) instead of COUNT(o.id) and reporting 1 order for customers who have none.
- Joining two one-to-many tables at once and double-counting sums.
Likely interviewer follow-ups
- Why does a WHERE condition on the right table change a LEFT JOIN's results?
- How do you find customers who have never placed an order? Give two ways.
- When would you rewrite a RIGHT JOIN as a LEFT JOIN?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply LEFT JOIN (Left Outer Join) & Preserving Unmatched Rows with hands-on SQL practice and interview Q&A.