Finding Missing Records (LEFT JOIN ... WHERE right.id IS NULL)
Detect orphans and gaps with anti-join patterns using LEFT JOIN + IS NULL.
In plain words
Match everything you can, then look at what's left with no partner.
Think of it like…
Taking the register against a class list: go down the list and put a tick next to each student who turns up; the names with no tick are the ones missing.
Where you’ve seen it
A nightly data-quality job checks that every paid order reached the warehouse: SELECT s.order_id FROM source_orders s LEFT JOIN dw.fact_orders f ON f.order_id = s.order_id WHERE s.status = 'paid' AND s.paid_at::date = current_date - 1 AND f.order_id IS NULL; Any rows returned trigger an alert before the morning revenue report goes out.
Words you’ll hear
- Anti-join
- Keeping rows of one table that have no match in another.
- Orphan row
- A child row whose parent (for example its customer) doesn't exist.
- IS NULL on the join key
- The reliable test: the right table's key is only NULL when no row matched.
- Reconciliation
- Comparing two sources of the same data to find what's missing on either side.
Visual diagram
Theory & example queries
Writing the pattern correctly
After a LEFT JOIN, a left row with no partner gets NULL in every right-side column. Test a right-side column that can never be NULL in a real match, normally its primary key or the join column: WHERE o.id IS NULL. If you test a nullable column such as o.coupon_code, you'll also get customers whose orders simply have no coupon.
Conditions on the right table must go in the ON clause. "Customers with no orders this year" is LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= '2026-01-01' WHERE o.id IS NULL. Putting the date in WHERE instead removes the NULL rows before IS NULL is checked, and the query returns nothing. NOT EXISTS expresses the same thing and most planners produce the same anti-join plan, so use whichever reads better.
| 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, p.nameFROM products p LEFT JOIN order_items oi ON oi.product_id = p.id AND oi.created_at >= '2026-11-01' AND oi.created_at < '2026-12-01'WHERE oi.order_id IS NULL;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output3 sample rows
| id | name |
|---|---|
| 18 | Canvas Tote |
| 27 | Desk Calendar |
| 41 | Travel Mug |
Note: Illustrative products with no matching order item during November 2026.
Key takeaway: IS NULL on the right table's key; right-table conditions in ON.
Orphans, expected records and two-way diffs
Three common uses. Orphans: child rows pointing at missing parents, which appear when there's no foreign key or data was loaded around it; join child to parent and keep parent.id IS NULL. Expected but missing: every active subscription should have an invoice this month, so LEFT JOIN subscriptions to this month's invoices. Missing dates: join a date spine to the events table to find days with no data at all.
To compare two copies of a dataset in both directions, use FULL OUTER JOIN and keep rows where either side is NULL: SELECT COALESCE(a.id, b.id) AS id, a.id IS NULL AS missing_in_a, b.id IS NULL AS missing_in_b FROM system_a a FULL JOIN system_b b ON b.id = a.id WHERE a.id IS NULL OR b.id IS NULL; On large tables, index the join columns on both sides, or the check becomes a pair of full scans plus a hash join.
SELECT s.idFROM subscriptions s LEFT JOIN invoices i ON i.subscription_id = s.id AND i.period = '2026-11'WHERE s.status = 'active' AND i.id IS NULL;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output3 sample rows
| id |
|---|
| 204 |
| 311 |
| 907 |
Note: Illustrative active subscriptions missing a November 2026 invoice.
Here s.status is in WHERE because it filters the left table.
Key takeaway: Left-table filters in WHERE, right-table filters in ON; FULL JOIN for two-way diffs.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Decide which side must exist
Put the table whose rows you want to report on the left: customers for "customers without orders", orders for "orders without customers".
FROM customers c LEFT JOIN orders oWhen to use it & trade-offs
What it is
To find rows in one table with no match in another, LEFT JOIN them and keep the rows where the right side came back empty: SELECT o.* FROM orders o LEFT JOIN customers c ON c.id = o.customer_id WHERE c.id IS NULL; returns orders whose customer doesn't exist, often called orphans. The same pattern finds customers with no orders, products never sold, and records that exist in one system but not another.
Why it matters: Most data-quality checks are "what's missing?" questions: payments without an invoice, events from deleted users, rows that didn't make it from the source into the warehouse. Interviewers ask for this pattern directly, and they watch for the two classic mistakes: testing the wrong column for NULL, and filtering the right table in WHERE.
When to reach for it
- Customers, products or users with no activity
- Orphaned child rows and broken references
- Checking a load or migration copied everything
When to avoid or be careful
- Testing a nullable right-side column for IS NULL
- Filtering the right table in WHERE
- Running reconciliation joins on unindexed columns of big tables
LEFT JOIN … IS NULL vs NOT EXISTS vs NOT IN
Strengths: Familiar; easy to switch into a FULL JOIN reconciliation.
Watch out: Easy to break with a right-side filter in WHERE.
Strengths: States the intent directly; NULL-safe.
Watch out: Can't return columns from the other table.
Strengths: Short to write.
Watch out: Returns no rows at all if the subquery yields a NULL.
Interview signals & numbers to know
When this topic comes up
- “Customers who never placed an order”
- “Find orphaned records”
- “Which rows didn't make it into the warehouse?”
- Data-quality checks
- LEFT JOIN with IS NULL
Benchmarks & rules of thumb
- 0 rows
- the expected result of a healthy orphan check
- 2
- places a condition can go: ON or WHERE
- 1 NULL
- enough to empty a NOT IN result
Common mistakes & follow-ups
Common interview mistakes
- Writing WHERE o.created_at >= '2026-01-01' AND o.id IS NULL and getting zero rows every time.
- Checking o.discount_code IS NULL and counting customers whose orders had no discount as having no orders.
- Running the orphan check once instead of scheduling it, so new orphans go unnoticed.
Likely interviewer follow-ups
- Find customers who signed up but never placed an order.
- How would you check every row in a source table reached the warehouse?
- Why does moving a condition from ON to WHERE change the result?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Finding Missing Records (LEFT JOIN ... WHERE right.id IS NULL) with hands-on SQL practice and interview Q&A.