COUNT Function
Count rows, count values, count unique values — and know the difference
Counting Rows and Values
COUNT is the most used aggregate function — but it comes in three very different forms, and confusing them is one of the most common analytics mistakes. Here's the critical difference:
COUNT(email) returns 95, not 100.How COUNT Works
Count rows or non-NULL values
- COUNT(*) counts ALL rows including NULLs
- COUNT(col) counts only non-NULL values in that column
- COUNT(DISTINCT col) counts unique non-NULL values
- Returns 0 for empty sets — never NULL
NULL Behavior
Critical COUNT + NULL rules
- COUNT(*) = 10 but COUNT(email) = 8 means 2 NULLs in email
- COUNT(DISTINCT NULL) = 0
- COUNT(*) with LEFT JOIN counts unmatched rows too
- Use COUNT(right_table.id) after LEFT JOIN for accurate counts
COUNT Mistakes
Frequent errors
- COUNT(col) in LEFT JOIN → 0 for unmatched, not NULL
- COUNT(*) vs COUNT(1) → identical performance
- Forgetting DISTINCT: COUNT(country) ≠ COUNT(DISTINCT country)
- COUNT in HAVING requires GROUP BY first
Result Behavior
What COUNT returns
- Always returns an integer ≥ 0
- Never returns NULL — empty set = 0
- With GROUP BY: one count per group
- Without GROUP BY: single row result
COUNT(*)Counts ALL rows (including those with NULLs)COUNT(column)Counts rows where column is NOT NULLCOUNT(DISTINCT col)Counts unique non-NULL values| id | product_id | rating | comment |
|---|---|---|---|
| 7001 | P101 | 5 | Excellent mouse, very comfy |
| 7002 | P101 | 4 | NULL |
| 7003 | P101 | 3 | Good but battery drains fast |
| 7004 | P102 | 5 | Solid build, worth every rupee |
| 7005 | P102 | 4 | Spacious — fits two monitors |
| 7006 | P103 | 4 | NULL |
| 7007 | P104 | 5 | Just works, durable braided cable |
| 7008 | P105 | 5 | Great sound for the price |
| 7009 | P105 | 4 | Battery could be better |
| 7010 | P107 | 3 | NULL |
| 7011 | P108 | 5 | Good notebooks, smooth paper |
| 7012 | P109 | 5 | Premium feel, noise cancellation is real |
| id | name | city | country | |
|---|---|---|---|---|
| C1001 | Aarav Sharma | [email protected] | Mumbai | India |
| C1002 | Sara Chen | [email protected] | Singapore | Singapore |
| C1003 | James Wilson | [email protected] | London | UK |
| C1004 | Maria Garcia | [email protected] | Madrid | Spain |
| C1005 | Yuki Tanaka | [email protected] | Tokyo | Japan |
| C1006 | Priya Patel | [email protected] | Delhi | India |
| C1007 | Alex Johnson | [email protected] | New York | USA |
| C1008 | Chen Wei | [email protected] | Shanghai | China |
| C1009 | Emma Brown | [email protected] | Sydney | Australia |
| C1010 | Omar Hassan | [email protected] | Dubai | UAE |
| C1011 | Lena Muller | [email protected] | Berlin | Germany |
| C1012 | Ravi Kumar | [email protected] | Bangalore | India |
Worked Example
Compare COUNT(*), COUNT(comment), and COUNT(DISTINCT rating) on the reviews table.
| total_reviews | with_comments | unique_ratings |
|---|---|---|
| 12 | 9 | 3 |
Key Concepts
Pro Tip
Counting is the foundation of data analysis. Knowing the difference between the three COUNT forms prevents silent miscounts.
When to Use
Total orders, active users, products per category, unique visitors, finding tables with missing data.
How many customers are there in total? How many unique countries do they come from? Show both in one query.