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
orders
order_idamountdiscount
ORD-00110010
ORD-002200NULL
ORD-00315015
ORD-004300NULL
ORD-00525025

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

QuestionCount to useAnswer 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
orders
order_idchannelamountdiscount
ORD-001web10010
ORD-002web200NULL
ORD-003web15015
ORD-004app300NULL
ORD-005app25025
Result
total_rowsrows_with_discountrows_missing_discount
532

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
orders
order_idchannelamountdiscount
ORD-001web10010
ORD-002web200NULL
ORD-003web15015
ORD-004app300NULL
ORD-005app25025
Result
channelorders_placedorders_with_discount
app21
web32

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

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

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.