SQL LEFT JOIN counts: return zero for missing matches
Keep customers with no matching orders in a SQL report. See how COUNT(*), COUNT(order_id), and a status filter change the result of a LEFT JOIN.
- Author
- Salon Rai
- 3 min read
A customer with no paid orders can disappear from a SQL report or show up with a count of one. Those are different bugs. To include every customer and count paid orders, start from the customer table, put the paid-order condition in the join, and count a non-nullable column from the order table.
Use three customers to expose both mistakes
A customer with no orders tests one case. A customer with an order that fails the filter tests another. The third customer should have more than one qualifying order, so you can catch a query that returns only a yes-or-no flag. Run these examples in a fresh SQLite database.
CREATE TABLE customer (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
status TEXT NOT NULL
);
INSERT INTO customer VALUES (1, 'Asha'), (2, 'Ben'), (3, 'Chen');
INSERT INTO orders VALUES
(11, 1, 'paid'), (12, 1, 'paid'), (13, 1, 'open'), (14, 2, 'open');
Asha has two paid orders, Ben has one open order, and Chen has never ordered. The report should have three rows with counts of 2, 0 and 0. Write that expected result down before adjusting the query. Seeing Asha’s correct count alone tells you very little about an outer join.
Inspect the rows before aggregating
A left join tests pairs of rows against its ON condition. When a customer has no matching pair, the result still includes a row for that customer with nulls for the right-hand columns. Look at the intermediate result without a group-by first. SQLite SELECT processing
SELECT c.name, o.id AS paid_order_id, o.status
FROM customer AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id AND o.status = 'paid'
ORDER BY c.id, o.id;
| Customer | Paid order ID | Status |
|---|---|---|
| Asha | 11 | paid |
| Asha | 12 | paid |
| Ben | NULL | NULL |
| Chen | NULL | NULL |
Counting all rows with COUNT(*) now gives 2, 1 and 1. That’s exactly the number of rows displayed for each name above. Counting o.id gives 2, 0 and 0 because the aggregate skips null arguments. Use a key that must be populated on every actual order. A nullable field such as shipped_at would answer a different question. SQLite aggregate function definitions
SELECT c.id, c.name, COUNT(o.id) AS paid_orders
FROM customer 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;
Put the filter at the stage it describes
Move o.status = 'paid' into a WHERE clause and only Asha remains. For Ben, the join by customer ID finds his open order and the later filter removes it. For Chen, the later comparison sees a null and doesn’t pass. Adding OR o.id IS NULL to the WHERE clause can bring Chen back while still losing Ben. That is the reason to keep both customers in your test data.
Conditions on the population itself belong in a different place. If the report covers active customers only, filtering on a customer-side active field in WHERE is appropriate. In plain language the two rules are “which customers get a line in the report?” and “which orders contribute to each line?” Translate them separately.
Count multiple statuses in a single pass
If you also need all orders and open orders, keep every order in the join and put the conditions inside the aggregates. This variation gives Ben a total of one and a paid count of zero. Chen’s total remains zero. It also keeps a newly introduced status in the overall count until you decide how to report it.
SELECT c.name,
COUNT(o.id) AS all_orders,
COUNT(CASE WHEN o.status = 'paid' THEN 1 END) AS paid_orders,
COUNT(CASE WHEN o.status = 'open' THEN 1 END) AS open_orders
FROM customer AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;
Leave the implicit ELSE NULL in those CASE expressions. Writing ELSE 0 inside COUNT would count misses too because zero is a non-null value. If the report later joins order lines, remember that two lines for one order can create two joined rows. Aggregate to order or customer level before bringing in another one-to-many relationship. The pandas merge example shows the same multiplication one row at a time.
Next steps
Keep all three customers as regression cases. Assert the identities of the customers alongside their counts. A test that only sums paid orders would pass when Asha is the sole row in the report, even though the team has lost every customer it meant to follow up with.

Written by