COUNT(*) vs
COUNT(column)
Master the subtle but critical difference between counting all rows and counting non-NULL values in SQL aggregate queries.
What is COUNT?
COUNT is an aggregate function in SQL. It takes a group of rows and returns a single number: how many items are in that group.
But what it counts depends on what you put inside the parentheses. This one detail causes more bugs than almost any other SQL concept.
Two Flavors of COUNT
The NULL Problem
In SQL, NULL does not mean zero or empty string. It means "unknown" or "missing." A student's email might be NULL because they never provided one.
The Core Rule
Formal Syntax
Side-by-Side Comparison
| Feature | COUNT(*) | COUNT(column) |
|---|---|---|
| Counts NULLs? | ✓ Yes | ✕ No |
| Input | Entire row | Single column value |
| With no rows | Returns 0 | Returns 0 |
| Performance | Often optimized | Must evaluate column |
| DISTINCT support | Not applicable | ✓ Yes |
| Use case | "How many rows?" | "How many have a value?" |
When to Use Which
"How many students are enrolled?"
You care about row existence, not column values.
"How many orders have a shipping date?"
You want to exclude missing data.
How to Choose
Example 1: Students Table
| id | name | |
|---|---|---|
| 1 | Alice | alice@school.edu |
| 2 | Bob | NULL |
| 3 | Carol | carol@school.edu |
| 4 | Dave | NULL |
| 5 | Eve | eve@school.edu |
Example 2: Orders Table
| order_id | customer | shipped_date |
|---|---|---|
| 101 | Alice | 2024-01-15 |
| 102 | Bob | NULL |
| 103 | Alice | 2024-01-18 |
| 104 | Carol | NULL |
| 105 | Bob | NULL |
| 106 | Dave | 2024-01-20 |
Example 3: GROUP BY + COUNT
| customer | total | shipped |
|---|---|---|
| Alice | 2 | 2 |
| Bob | 2 | 0 |
| Carol | 1 | 0 |
| Dave | 1 | 1 |
Bonus: COUNT(DISTINCT)
| id | city |
|---|---|
| 1 | NYC |
| 2 | NYC |
| 3 | NULL |
| 4 | LA |
| 5 | NULL |
Decision Flowchart
Common Mistakes
Summary Cheat Sheet
| Expression | NULLs? | Duplicates? | Result |
|---|---|---|---|
| COUNT(*) | Included | Included | All rows |
| COUNT(col) | Excluded | Included | Non-NULL rows |
| COUNT(DISTINCT col) | Excluded | Excluded | Unique values |
| COUNT(1) | Included | Included | Same as COUNT(*) |
Question 1 of 5
A table has 10 rows. The phone column contains NULL in 4 of those rows. What does COUNT(phone) return?
Question 2 of 5
Which statement is always true?
Question 3 of 5
A city column has values: 'NYC', 'NYC', NULL, 'LA', NULL. What does COUNT(DISTINCT city) return?
Question 4 of 5
An age column has values: 18, 0, NULL, 21, 0. What does COUNT(age) return?
Question 5 of 5
You want to know how many customers placed at least one order. The orders table has a customer_id column (no NULLs). Which query is best?