Why it happens: COUNT with LEFT JOIN returns 1 instead of 0
WITH categories(id, name) AS (
VALUES (1, 'Laptops'), (2, 'Audio'), (3, 'Monitors')
),
products(id, category_id) AS (
VALUES (101, 1), (102, 1), (103, 2)
)
SELECT c.name, COUNT(*) AS products
FROM categories AS c
LEFT JOIN products AS p
ON p.category_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;| name | products |
|---|---|
| Laptops | 2 |
| Audio | 1 |
| Monitors | 1 |
Check the result with small data
Team B exists even though it has no members yet.
WITH teams(id, name) AS (VALUES (1,'A'),(2,'B')),
members(team_id, name) AS (VALUES (1,'Amy'))
SELECT e.name, COUNT(m.name) AS people
FROM teams e LEFT JOIN members m ON m.team_id=e.id
GROUP BY e.id, e.name ORDER BY e.id;| name | people |
|---|---|
| A | 1 |
| B | 0 |
COUNT(m.name) returns zero for B because the right-hand column is NULL. COUNT(*) would return one, because it also counts the row the LEFT JOIN keeps.
Check your understanding: Why do we need to count a column from the right table?
Show answer
Because in rows without a match that column is NULL, and COUNT(column) does not count NULLs. COUNT(*) also counts the row preserved by the LEFT JOIN and would return 1 instead of 0.
How to fix it in SQL
WITH categories(id, name) AS (
VALUES (1, 'Laptops'), (2, 'Audio'), (3, 'Monitors')
),
products(id, category_id) AS (
VALUES (101, 1), (102, 1), (103, 2)
)
SELECT c.name, COUNT(p.id) AS products
FROM categories AS c
LEFT JOIN products AS p
ON p.category_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;| name | products |
|---|---|
| Laptops | 2 |
| Audio | 1 |
| Monitors | 0 |
Rule of thumb: after a LEFT JOIN, use COUNT(*) if you really want to count the preserved result rows; use COUNT(right_table.non_null_column) if you want to count real matches.
How to check it
Find an entity with no matches: its count should be 0 after the fix.
How to avoid it next time
COUNT(*) counts rows; COUNT(column) skips NULLs.
Related cases worth checking
If you also need to count only products that meet a condition, putting that condition in WHERE can remove Monitors and turn the effect of the LEFT JOIN into that of an INNER JOIN for that filter. When you want to keep the zero, it is usually clearer to move the related condition into ON and keep counting a non-null column from the right table.
The padded row exists, but it does not represent a transaction
The customer with no orders still has a row in the intermediate result of the left join, with null order columns. That is why COUNT(*) finds a row: it counts the structure it received, not whether an order exists. Counting a required order key tells that padded row apart from real transactions.
Do not count an optional column such as the shipping date. An existing order that has not shipped yet may have it null and drop out of the count. If you want shipped orders, that behaviour may be fine, but the metric must say so. If you want all orders, the required key is the right thing to count.
Also check any joins that come after. If each order has three lines, counting its key after joining the lines counts it three times. Summarising orders before that join can be clearer than patching every symptom with distinct counts. In your tests, include zero, one and several orders, and at least one order with missing optional data. If you filter order types, put the filter where it keeps the population the report promises to show.
Technical questions: PostgreSQL: aggregate functions (opens in a new tab).