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 44/ 160
03. Joins & Relational Querying

LEFT JOIN (Left Outer Join) & Preserving Unmatched Rows

Keep every left-side row and fill missing right-side columns with NULL.

BeginnerLEFT · Preserve · NULL3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

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

Customers and their order countsQuery example
1Input dataStart from the source rows or state that the statement sees.
customers
3 rows
customers sample rows
idnameemailcity
1Mayamaya@example.comPune
2Raviravi@example.comDelhi
3Noornoor@example.comMumbai
orders
3 rows
orders sample rows
idcustomer_idstatusamountcoupon_code
1011paid249NULL
1022paid120WELCOME10
1031shipped89.5NULL
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1SELECT2  c.id,3  COUNT(o.id) AS orders4FROM5  customers c6  LEFT JOIN orders o ON o.customer_id = c.id7GROUP BY8  c.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 customer order counts3 sample rows
Representative customer order counts
idorders
74
121
230

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.

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

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.

Paid orders per customer, keeping everyone

… 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.

Key takeaway: Right-table conditions go in ON, or the LEFT JOIN becomes an INNER JOIN.
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 1Left table

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.

SQL at this step
1FROM2  customers c
What to watch at this step
Kept
all rows
Tip: use ← → to move between steps

When 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

INNER JOIN
Best for: Only rows that have a match on both sides

Strengths: Simpler results with no NULL handling.

Watch out: Silently drops unmatched rows.

LEFT JOIN
Best for: All rows from one table, with related data when it exists

Strengths: Keeps zero-match entities in reports.

Watch out: Needs NULL handling, and fans out when the right side has many matches.

NOT EXISTS
Best for: Only the rows that have no match (anti-join)

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?
Interview tip: Draw two tiny tables (three rows each) and walk through the output, including one unmatched row. Then mention the ON-vs-WHERE trap: it shows you've debugged real LEFT JOINs.

Knowledge check

Loading quiz

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.

Open SQL Practice →SQL interview Q&A
Previous topic
INNER JOIN Mechanics & Multi-Table Joins
Next topic
Finding Missing Records (LEFT JOIN ... WHERE right.id IS NULL)