Finding unmatched rows
Concepts covered: Outer Joins
The NULLs a LEFT JOIN adds carry information. A padded row exists only because its left row found no partner. So a query that keeps only the padded rows answers a question no INNER JOIN can: which rows have nothing on the other side? Customers who never ordered fit that shape. So do products nobody bought, and refunds that point at an absent order.
The recipe has 2 parts. LEFT JOIN from the table whose rows you are checking. Then, in WHERE, keep the rows where the right table's key IS NULL. This shape is called an anti-join, because it returns the left rows the join could not match.
SELECT c.name
FROM customers c
LEFT JOIN orders o
ON c.cust_id = o.cust_id
WHERE o.order_id IS NULL| cust_id | name |
|---|---|
| C1 | Alice |
| C2 | Bob |
| C3 | Carol |
| C4 | Dave |
| order_id | cust_id |
|---|---|
| O1 | C1 |
| O2 | C1 |
| O3 | C3 |
Apply the recipe to customers and orders, and follow the order the database works in. The join runs first and builds 5 rows. Alice has 2 and Carol has 1, and Bob and Dave each get a padded row. WHERE runs next and tests o.order_id IS NULL on each of the 5. Alice's and Carol's rows hold real order ids, so the test is false. Bob's and Dave's hold NULL, so the test is true. The result is 2 names: Bob and Dave.
Test a column that is never NULL
Which column you test matters. o.order_id is the primary key of orders, so no real order has a NULL there. A NULL in that column can only come from padding. Now suppose orders also had a nullable coupon column and you tested o.coupon IS NULL instead. Every customer who ordered without a coupon would pass the test and be reported as having no orders. Test the right table's primary key, or the join column itself.
Missing rows and orphaned rows
The same query shape finds 2 different problems, and the difference is which table sits on the left. Put the parent table on the left and you find parents with no children: customers with no orders. Those rows are often a legitimate business fact. Put the child table on the left and you find children with no parent. A child row whose foreign key matches no primary key is called an orphan. An orphan signals a fault: usually its parent was deleted or never loaded, though sometimes the parent was written with a different key.
The store also keeps a refunds table, where each refund names the order it reverses. Every order_id in it should exist in orders. This query checks.
SELECT
r.refund_id,
r.order_id,
r.amount
FROM refunds r
LEFT JOIN orders o
ON r.order_id = o.order_id
WHERE o.order_id IS NULL| refund_id | order_id | amount |
|---|---|---|
| R1 | O2 | 30 |
| R2 | O9 | 45 |
| R3 | O1 | 20 |
| order_id | cust_id | total |
|---|---|---|
| O1 | C1 | 50 |
| O2 | C1 | 30 |
| O3 | C3 | 75 |
| refund_id | order_id | amount |
|---|---|---|
| R2 | O9 | 45 |
Refunds R1 and R3 are fine, since R1 reverses O2 and R3 reverses O1. R2 names order O9, and orders has no O9. The join pads R2 with NULL, the WHERE keeps it, and the result is one row: R2, O9, 45. The SELECT list reads r.order_id, the refund's own copy of the key, because o.order_id is NULL on every row this query returns.
- Missing (parent on the left)
- Finds parents with no children
- Example: customers with no orders
- Usually a real business fact
- Orphaned (child on the left)
- Finds children with no parent
- Example: refunds naming an absent order
- A data fault to investigate
A products table has one row per product. order_items has one row per product sold, with a product_id foreign key. Which query lists the products that have never been sold?
- FROM products p LEFT JOIN order_items i ON p.product_id = i.product_id WHERE i.product_id IS NULL
- FROM order_items i LEFT JOIN products p ON i.product_id = p.product_id WHERE p.product_id IS NULL
- FROM products p INNER JOIN order_items i ON p.product_id = i.product_id WHERE i.product_id IS NULL
- FROM products p LEFT JOIN order_items i ON p.product_id = i.product_id WHERE p.product_id IS NULL
Why orphans happen at all
A foreign key constraint would reject R2 at insert time. Analytical tables are often loaded by separate jobs on separate schedules, with nothing enforcing the relationship between them. A refund can land an hour before its order does. A delete can remove an order and leave its refunds behind. An anti-join run on a schedule catches this before a dashboard does. An INNER JOIN from refunds to orders would drop R2 without a word, and total refunds would come up 45 short.
Before running an anti-join, write it as one sentence: "rows of the left table that have nothing on the right". If that sentence names the rows you want, the tables are in the right order. If it names the opposite set, swap them.
About this lesson
This section is part of the Joins: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.