Sub topic 3
COUNT(*), COUNT(column) and COUNT(DISTINCT)
Assume you know SELECT, WHERE, ORDER BY, tables, joins, and NULL. This chapter focuses on how the three common forms of COUNT answer different questions.
What each form counts
COUNT is an aggregate function. It reads several rows and returns one numeric summary.
COUNT(*)counts rows.COUNT(column)counts rows where that column is notNULL.COUNT(DISTINCT column)counts different non-NULLvalues in that column.
Consider this logical table:
| order_id | customer_id | shipped_at |
|---|---|---|
| 101 | 7 | 2026-01-10 |
| 102 | 7 | `NULL` |
| 103 | 9 | 2026-01-11 |
| 104 | `NULL` | `NULL` |
The counts are different:
SELECT COUNT(*) AS all_orders,
COUNT(shipped_at) AS shipped_orders,
COUNT(customer_id) AS orders_with_customer,
COUNT(DISTINCT customer_id) AS distinct_customers
FROM orders;| all_orders | shipped_orders | orders_with_customer | distinct_customers |
|---|---|---|---|
| 4 | 2 | 3 | 2 |
COUNT(*) sees four rows, regardless of their contents. COUNT(shipped_at) sees only the two non-NULL dates. COUNT(customer_id) sees three non-NULL customer IDs. COUNT(DISTINCT customer_id) sees the values 7 and 9; repeated 7 is counted once, and NULL is ignored.
How grouping changes the result
Without GROUP BY, the query has one group: the entire input table. With GROUP BY, SQL calculates each count separately for every group.
SELECT customer_id,
COUNT(*) AS order_count,
COUNT(shipped_at) AS shipped_order_count,
COUNT(DISTINCT shipped_at) AS shipping_days
FROM orders
GROUP BY customer_id
ORDER BY customer_id;Here, shipping_days counts distinct date values per customer. Two orders shipped on the same date contribute one shipping day. A customer whose orders all have shipped_at = NULL receives zero for both COUNT(shipped_at) and COUNT(DISTINCT shipped_at).
A grouped query can include a grouping column and aggregate expressions. Each output row represents one group.
Choosing the correct count
Use the question being asked to choose the expression:
| Question | Expression |
|---|---|
| How many rows are present? | `COUNT(*)` |
| How many rows have a value in this column? | `COUNT(column)` |
| How many different values are present? | `COUNT(DISTINCT column)` |
A frequent mistake is using COUNT(customer_id) to count orders. That undercounts orders whose customer ID is missing. Use COUNT(*) when the unit being counted is the row.
Another mistake appears with joins. In a LEFT JOIN, an unmatched right-side row produces NULL values for the right table. Therefore, count a non-nullable right-table key to count matches:
SELECT c.customer_id,
COUNT(o.order_id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
GROUP BY c.customer_id;COUNT(o.order_id) returns zero for a customer with no orders. COUNT(*) would count the preserved customer row and return one.
Exercise
A group contains five rows. The target column has values A, A, B, NULL, and NULL. What are COUNT(*), COUNT(column), and COUNT(DISTINCT column)?
Show answer
COUNT(*) is 5, COUNT(column) is 3, and COUNT(DISTINCT column) is 2.