SQL LEFT JOIN filters: ON versus WHERE, with examples

By StringClass · Published

An ON condition determines which right-hand rows may match. A WHERE condition filters the joined result. To keep every customer while matching only paid orders, put the paid-order condition in ON.

Before you start: You can read SELECT, joins, WHERE, and GROUP BY. Examples use SQLite-compatible SQL and integer cents, without external databases or private data.

State which customers must remain

Suppose a report should list every customer and show their paid orders when they have any. Some customers have only pending orders; others have no orders at all. Both groups still belong in the report.

Put the paid-order restriction in the ON clause when it defines which orders may match. Put it in WHERE when you intend to filter the joined output and exclude customers without qualifying orders. Those are different questions, even though both queries use LEFT JOIN.

The examples use SQLite-compatible SQL, synthetic customer data, and integer cents. Run the setup once in a fresh database, then run each SELECT independently.

Create data that exposes the difference

CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  status TEXT NOT NULL,
  amount_cents INTEGER NOT NULL
);
INSERT INTO customers VALUES (1, 'Ada'), (2, 'Bo'), (3, 'Cy'), (4, 'Di');
INSERT INTO orders VALUES
  (10, 1, 'paid', 1200),
  (11, 1, 'pending', 800),
  (12, 2, 'pending', 500),
  (13, 4, 'paid', 700),
  (14, 4, 'paid', 300);

Ada has one paid and one pending order. Bo has only a pending order. Cy has no orders. Di has two paid orders. The last case is important: keeping customers does not imply one output row per customer.

Filter matches with ON

SELECT c.id AS customer_id, c.name, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o
  ON o.customer_id = c.id AND o.status = 'paid'
ORDER BY c.id, o.id;
customer_id name order_id
1 Ada 10
2 Bo NULL
3 Cy NULL
4 Di 13
4 Di 14

Only paid orders qualify as matches. Bo and Cy have no such match, so the left join retains each customer with NULL order columns. Di appears twice because two qualifying orders match.

This query answers: “List all customers, together with each paid order they have.”

Filter the joined rows with WHERE

SELECT c.id AS customer_id, c.name, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
WHERE o.status = 'paid'
ORDER BY c.id, o.id;
customer_id name order_id
1 Ada 10
4 Di 13
4 Di 14

The join first matches orders regardless of status. The WHERE condition then removes pending rows. It also removes Cy's NULL-extended row: comparing NULL with 'paid' does not produce true, and WHERE keeps only true conditions.

Bo has a real pending order, so the join does not manufacture an additional unmatched row for him. Once his pending row is filtered out, nothing remains. For this predicate, the result is equivalent to an inner join restricted to paid orders.

Why adding OR IS NULL is not the same repair

SELECT c.id AS customer_id, c.name, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
WHERE o.status = 'paid' OR o.id IS NULL
ORDER BY c.id, o.id;

This restores Cy, who had no matching order at all. It still excludes Bo, whose pending order matched before the filter. His o.id is 12, not NULL. Use the ON-filtered version when the original requirement is to retain every customer and match only paid orders.

Using o.id IS NULL is meaningful here because orders.id is a non-null primary key. A nullable business column would not reliably distinguish an unmatched row from a matched row containing a missing value.

Count qualifying orders, including zero

To produce one row per customer instead, group the ON-filtered result and count a non-null order identifier:

SELECT c.id AS customer_id, c.name, COUNT(o.id) AS paid_orders
FROM customers AS c
LEFT JOIN orders AS o
  ON o.customer_id = c.id AND o.status = 'paid'
GROUP BY c.id, c.name
ORDER BY c.id;
customer_id name paid_orders
1 Ada 1
2 Bo 0
3 Cy 0
4 Di 2

COUNT(o.id) ignores NULL identifiers. COUNT(*) counts the unmatched placeholder row too, giving Bo and Cy a count of one. Choose the expression according to what you are counting.

Filters on the left table still have their own meaning. For example, WHERE c.id >= 2 deliberately restricts the customer list; moving that restriction into ON would leave the other customers in the output as unmatched rows. Do not move every WHERE condition mechanically.

Check the requirement before changing the query

Add a pending order for Cy. The ON-filtered query should still retain Cy with a NULL order, and the grouped query should still report zero paid orders. The OR o.id IS NULL version will now lose Cy too.

Next, add a second customer named Ada with a new ID and no orders. The grouped query should retain a separate zero-count row because identity comes from the ID, not the name.

For reports combining several one-to-many relationships, continue with SQL duplicate joins and inflated totals. Keeping unmatched customers and avoiding repeated contributions to a sum are related but separate requirements.

Reference: SQLite's SELECT documentation describes join constraints, NULL-extended outer-join rows, WHERE filtering, and grouping.