COUNT Function

Count rows, count values, count unique values — and know the difference

Intermediate 12 minCOUNTaggregate

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(*) counts all rows — Including rows with NULLs. It answers "how many rows exist?" and is the only aggregate function that includes NULL values in its count. Use this for total row counts.
COUNT(column) skips NULLs — Counts only rows where that specific column is NOT NULL. If a table has 100 rows but 5 have NULL in the email column, COUNT(email) returns 95, not 100.
COUNT(DISTINCT column) counts unique values — Returns the number of unique non-NULL values. 100 rows with 10 distinct countries gives you 10. Useful for understanding cardinality and data distribution at a glance.
Knowledge Canvas

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
Syntax
Syntax Template
1-- Count all rows (including NULLs)
2SELECT
3 COUNT(*)
4FROM table;
5
6-- Count non-NULL values in a column
7SELECT
8 COUNT(column)
9FROM table;
10
11-- Count unique non-NULL values
12SELECT
13 COUNT(DISTINCT column)
14FROM table;
COUNT(*)Counts ALL rows (including those with NULLs)
COUNT(column)Counts rows where column is NOT NULL
COUNT(DISTINCT col)Counts unique non-NULL values
Sample Data
reviews
12 rows
idproduct_idratingcomment
7001P1015Excellent mouse, very comfy
7002P1014NULL
7003P1013Good but battery drains fast
7004P1025Solid build, worth every rupee
7005P1024Spacious — fits two monitors
7006P1034NULL
7007P1045Just works, durable braided cable
7008P1055Great sound for the price
7009P1054Battery could be better
7010P1073NULL
7011P1085Good notebooks, smooth paper
7012P1095Premium feel, noise cancellation is real
customers
12 rows
idnameemailcitycountry
C1001Aarav Sharma[email protected]MumbaiIndia
C1002Sara Chen[email protected]SingaporeSingapore
C1003James Wilson[email protected]LondonUK
C1004Maria Garcia[email protected]MadridSpain
C1005Yuki Tanaka[email protected]TokyoJapan
C1006Priya Patel[email protected]DelhiIndia
C1007Alex Johnson[email protected]New YorkUSA
C1008Chen Wei[email protected]ShanghaiChina
C1009Emma Brown[email protected]SydneyAustralia
C1010Omar Hassan[email protected]DubaiUAE
C1011Lena Muller[email protected]BerlinGermany
C1012Ravi Kumar[email protected]BangaloreIndia

Worked Example

Compare COUNT(*), COUNT(comment), and COUNT(DISTINCT rating) on the reviews table.

SQL
1SELECT
2 COUNT(*) AS total_reviews,
3 COUNT(comment) AS with_comments,
4 COUNT(DISTINCT rating) AS unique_ratings
5FROM reviews;
COUNT(*) = 12 (all rows). COUNT(comment) = 9 (three reviews have NULL comments). COUNT(DISTINCT rating) = 3 (ratings are 3, 4, and 5).
Output
1 row
total_reviewswith_commentsunique_ratings
1293

Key Concepts

1COUNT(*) counts all rows including those with NULLs — COUNT(column) counts only non-NULL values in that column
2COUNT(DISTINCT col) counts unique non-NULL values
3Mixing up the three COUNT forms causes silent miscounts
4COUNT(*) with GROUP BY counts per group, not the whole table

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.

Challenge

Solve the problem below

How many customers are there in total? How many unique countries do they come from? Show both in one query.

Your Query