01 / 21
id name

COUNT(*) vs
COUNT(column)

Master the subtle but critical difference between counting all rows and counting non-NULL values in SQL aggregate queries.

SQL

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

COUNT(*)
Counts every row in the group, no matter what values the columns hold: data, empty strings, or even NULL.
COUNT(column)
Counts only the rows where that specific column is NOT NULL. Rows with NULL in that column are silently skipped.

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.

NULL ≠ 0
Zero is a known value. NULL means no value exists at all.
NULL ≠ ''
An empty string is a known, stored value. NULL means nothing was stored.

The Core Rule

COUNT(*) Row 1: Alice, 90 Row 2: Bob, NULL Row 3: Carol, 85 Row 4: Dave, NULL ✓ ✓ ✓ ✓ = 4 COUNT(score) Row 1: Alice, 90 Row 2: Bob, NULL Row 3: Carol, 85 Row 4: Dave, NULL ✓ ✕ ✓ ✕ = 2

Formal Syntax

-- Count every row in the table SELECT COUNT(*) FROM students; -- Count non-NULL values in a specific column SELECT COUNT(email) FROM students; -- Count distinct non-NULL values SELECT COUNT(DISTINCT city) FROM students;

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

Use COUNT(*)
"How many orders were placed?"
"How many students are enrolled?"
You care about row existence, not column values.
Use COUNT(column)
"How many students provided an email?"
"How many orders have a shipping date?"
You want to exclude missing data.

How to Choose

1
Read the question carefully. Does it ask about rows or about a specific column's presence?
2
Check for NULLs. Could the column have missing values? If yes, COUNT(*) and COUNT(column) will differ.
3
Decide your intent. "Total rows" → COUNT(*). "Rows with data in X" → COUNT(X).
4
Need unique values? Use COUNT(DISTINCT column) to remove duplicates and NULLs simultaneously.

Example 1: Students Table

idnameemail
1Alicealice@school.edu
2BobNULL
3Carolcarol@school.edu
4DaveNULL
5Eveeve@school.edu
SELECT COUNT(*) AS total_students, COUNT(email) AS has_email FROM students;
total_students = 5 has_email = 3

Example 2: Orders Table

order_idcustomershipped_date
101Alice2024-01-15
102BobNULL
103Alice2024-01-18
104CarolNULL
105BobNULL
106Dave2024-01-20
SELECT COUNT(*) AS total_orders, COUNT(shipped_date) AS shipped FROM orders;
total_orders = 6 shipped = 3

Example 3: GROUP BY + COUNT

SELECT customer, COUNT(*) AS total, COUNT(shipped_date) AS shipped FROM orders GROUP BY customer;
customertotalshipped
Alice22
Bob20
Carol10
Dave11

Bonus: COUNT(DISTINCT)

idcity
1NYC
2NYC
3NULL
4LA
5NULL
SELECT COUNT(*) AS total, COUNT(city) AS has_city, COUNT(DISTINCT city) AS unique_cities FROM people;
total = 5 has_city = 3 unique_cities = 2

Decision Flowchart

What do you want to count? Rows Values COUNT(*) Includes NULLs Need unique values? No Yes COUNT(column) COUNT (DISTINCT col) Skips NULLs Skips NULLs + deduplicates Total row count

Common Mistakes

✕ Assuming They're Equal
Using COUNT(*) when you mean COUNT(column), or vice versa. They only match when no NULLs exist in that column.
✕ Forgetting NULL ≠ 0
A column value of 0 or empty string is counted by COUNT(column). Only NULL is skipped.
✕ COUNT(1) Confusion
COUNT(1) behaves like COUNT(*): the literal 1 is never NULL, so every row is counted. It's not a different feature.

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?