COUNT(*) vs COUNT(column)
Concepts covered: Counting
You've used both COUNT(*) and COUNT(column). On a column with no gaps they return the same number, which hides the fact that they ask different questions. COUNT(*) counts rows. COUNT(column) counts the rows where that column holds a value, and it steps over every NULL.
Here are the shop's 5 orders again, this time with their discount column: the amount taken off by a coupon. 3 orders have a discount recorded and 2 have NULL. Play the query to watch both counts walk the same rows.
SELECT
COUNT(*) AS total_rows,
COUNT(discount) AS rows_with_discount
FROM orders| order_id | amount | discount |
|---|---|---|
| ORD-001 | 100 | 10 |
| ORD-002 | 200 | NULL |
| ORD-003 | 150 | 15 |
| ORD-004 | 300 | NULL |
| ORD-005 | 250 | 25 |
total_rows is 5 and rows_with_discount is 3. Read the star in COUNT(*) as "the row itself". A row exists whether or not its cells are filled, so a row full of NULLs still counts as 1 and no NULL can ever make COUNT(*) smaller. COUNT(discount) looks at one cell per row and adds 1 only when that cell has something in it.
Which count answers which question
| Question | Count to use | Answer here |
|---|---|---|
| How many orders were placed? | COUNT(*) | 5 |
| How many orders have a discount recorded? | COUNT(discount) | 3 |
| How many orders have no discount recorded? | COUNT(*) - COUNT(discount) | 2 |
Subtracting one count from the other, as the third row does, gives the number of NULLs in a column without writing a WHERE, so it can sit beside other aggregates in the same query.
SELECT
COUNT(*) AS total_rows,
COUNT(discount) AS rows_with_discount,
COUNT(*) - COUNT(discount) AS rows_missing_discount
FROM orders| order_id | channel | amount | discount |
|---|---|---|---|
| ORD-001 | web | 100 | 10 |
| ORD-002 | web | 200 | NULL |
| ORD-003 | web | 150 | 15 |
| ORD-004 | app | 300 | NULL |
| ORD-005 | app | 250 | 25 |
| total_rows | rows_with_discount | rows_missing_discount |
|---|---|---|
| 5 | 3 | 2 |
The same rule inside every group
GROUP BY changes nothing about the rule. Each group gets its own COUNT(*) and its own COUNT(discount), and the second one skips the NULLs inside that group. The orders also carry a channel column saying whether they came from the web or the app.
SELECT
channel,
COUNT(*) AS orders_placed,
COUNT(discount) AS orders_with_discount
FROM orders
GROUP BY channel| order_id | channel | amount | discount |
|---|---|---|---|
| ORD-001 | web | 100 | 10 |
| ORD-002 | web | 200 | NULL |
| ORD-003 | web | 150 | 15 |
| ORD-004 | app | 300 | NULL |
| ORD-005 | app | 250 | 25 |
| channel | orders_placed | orders_with_discount |
|---|---|---|
| app | 2 | 1 |
| web | 3 | 2 |
The web channel has 3 orders and 2 of them carry a discount. The app channel has 2 orders and 1 with a discount. Anyone reading only the discount column would conclude the shop took 3 orders in total, when it took 5.
The gap between the 2 columns is the number of orders with no discount recorded in that channel: 1 for web and 1 for app.
2 details that follow from the rule
- COUNT(DISTINCT column) skips NULLs as well. A region column holding US, NULL, EU, NULL, US has 2 distinct values to count, so the answer is 2.
- COUNT never returns NULL. When every cell in the column is NULL, COUNT(column) returns 0, and that 0 is a real measurement: zero rows had a value.
How this goes wrong at work
The classic mistake is writing COUNT(some_column) out of habit when the question was about rows. The query runs, the number looks plausible, and it's too low by exactly the number of NULLs in that column. A table of 2,000,000 signups with a 3% NULL rate in referral_code would be reported as 1,940,000 signups.
The reverse mistake happens too: using COUNT(*) to answer "how many customers gave us a phone number?" and counting the customers who gave none. Before typing either one, say the question aloud. If it is about rows, use the star. If it's about a recorded value, name the column.
One more prediction, this time mixing the count with the filter from the previous section.
customers has 4 rows, and phone is NULL in 2 of them. What does this query return?
SELECT
COUNT(*) AS matching_rows,
COUNT(phone) AS phones_counted
FROM customers
WHERE phone IS NULL- matching_rows = 2 and phones_counted = 0
- matching_rows = 2 and phones_counted = 2
- matching_rows = 4 and phones_counted = 2
- matching_rows = 0 and phones_counted = 0
COUNT is the gentlest place to meet this behaviour, because a count of skipped rows is still a sensible number. The other aggregates skip NULLs in the same way, and for an average the skipped rows change what the number means.
About this lesson
This section is part of the NULL Values: Beginner 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.