Sub topic 7
Filtering in `ON` versus `WHERE`
Assume you know INNER JOIN, LEFT JOIN, and NULL. The examples use these tables:
customers(customer_id, name)orders(order_id, customer_id, total)
Ada has orders, Ben has one order, Cleo has none, and order 104 has no customer.
The key difference
A LEFT JOIN preserves every row from its left table. If no right-side row matches, SQL adds a result row with NULL in the right-side columns.
The ON clause decides which right-side rows can match. The WHERE clause filters the complete result after the join has happened.
-- The condition is part of matching.
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
AND o.total > 30;This keeps every customer. Orders below or equal to 30 do not match, so their order columns become NULL.
Expected result:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ben | 103 | 35.00 |
| Cleo | NULL | NULL |
The same condition in WHERE has a different effect:
-- The condition is applied after the join.
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total > 30;The WHERE clause rejects rows where o.total is NULL. It also rejects orders with totals of 30 or less. Cleo disappears, so this query behaves like an inner join for this condition.
Expected result:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ben | 103 | 35.00 |
A visual model
Finding unmatched rows
This difference makes a LEFT JOIN useful for finding rows with no match. Join normally, then keep only rows whose right-table key is NULL.
SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;Result:
| name |
|---|
| Cleo |
Use a right-table column that cannot be NULL for a real order. order_id is a primary key, so a NULL value means SQL created a padded row rather than finding an order.
The equivalent NOT EXISTS query often states the intent more directly:
SELECT c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);Common errors
Do not move every condition into WHERE automatically. This common pattern removes customers without qualifying orders:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total >= 50;If you need all customers and only want orders worth at least 50, use:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
AND o.total >= 50;With several joins, check each condition against the rows you intend to preserve. A later WHERE condition on a nullable table can undo an earlier outer join.
Exercise
You need every customer in the result, but only orders with a total greater than 25 may match. Should the total condition go in ON or WHERE?
Show answer
Put o.total > 25 in the ON clause. This limits matching orders while preserving customers with no qualifying order as NULL-padded rows.