SQL COUNT in Detail
What you will learn
- The difference between
COUNT(*),COUNT(column)andCOUNT(DISTINCT column) - How
NULLis handled by each - How to count different values per group
- How to check that counts add up
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. Three ways to count
| Form | Counts |
|---|---|
COUNT(*) |
all rows |
COUNT(column) |
rows where the column is not NULL |
COUNT(DISTINCT column) |
the different values of the column, ignoring NULL |
Run all three on a column that has missing values and one that does not:
SELECT COUNT(*) AS all_rows,
COUNT(phone) AS with_phone,
COUNT(DISTINCT city) AS different_cities,
COUNT(DISTINCT country) AS different_countries
FROM customers;
all_rows with_phone different_cities different_countries
-------- ---------- ---------------- -------------------
420 336 25 16
(1 row)
SELECT COUNT(*) AS all_rows,
COUNT(note) AS with_note,
COUNT(DISTINCT note) AS different_notes,
COUNT(DISTINCT table_name) AS different_tables
FROM audit_log;
all_rows with_note different_notes different_tables
-------- --------- --------------- ----------------
120 91 4 4
(1 row)
SELECT COUNT(*) AS all_rows,
COUNT(guardian_name) AS with_guardian,
COUNT(DISTINCT guardian_name) AS different_guardians,
COUNT(DISTINCT enrolled_year) AS different_years
FROM students;
all_rows with_guardian different_guardians different_years
-------- ------------- ------------------- ---------------
360 322 305 4
(1 row)
Read the numbers: the rows that have a value in the column are fewer than all rows, and the different values are fewer still (some values repeat).
2. COUNT(*) never skips a row
COUNT(*) counts rows, whatever they hold. COUNT(column) skips the rows where that column is empty. The difference between them is exactly the number of NULLs:
SELECT COUNT(*) - COUNT(phone) AS rows_without_phone,
(SELECT COUNT(*) FROM customers WHERE phone IS NULL) AS checked_with_is_null
FROM customers;
rows_without_phone checked_with_is_null
------------------ --------------------
84 84
(1 row)
SELECT COUNT(*) - COUNT(note) AS rows_without_note,
(SELECT COUNT(*) FROM audit_log WHERE note IS NULL) AS checked_with_is_null
FROM audit_log;
rows_without_note checked_with_is_null
----------------- --------------------
29 29
(1 row)
SELECT COUNT(*) - COUNT(guardian_name) AS rows_without_guardian,
(SELECT COUNT(*) FROM students WHERE guardian_name IS NULL) AS checked_with_is_null
FROM students;
rows_without_guardian checked_with_is_null
--------------------- --------------------
38 38
(1 row)
Both ways give the same number, a handy cross-check.
3. Different values per group
COUNT(DISTINCT column) also works inside a GROUP BY. It answers "how many different ... in each group?":
How many different cities does each country have among the customers?
SELECT country, COUNT(*) AS customers, COUNT(DISTINCT city) AS cities
FROM customers GROUP BY country ORDER BY customers DESC, country LIMIT 5;
country customers cities
-------------- --------- ------
United Kingdom 35 1
Turkey 32 2
Brazil 30 2
Kenya 30 2
Peru 29 2
(5 rows)
How many different tables does each kind of audit action touch?
SELECT action, COUNT(*) AS entries, COUNT(DISTINCT table_name) AS tables_touched
FROM audit_log GROUP BY action ORDER BY action;
action entries tables_touched
------ ------- --------------
delete 12 4
insert 38 4
update 70 4
(3 rows)
How many different sections does each term have, and how many different teachers teach in it?
SELECT term, COUNT(*) AS sections, COUNT(DISTINCT teacher_id) AS teachers
FROM sections GROUP BY term ORDER BY term;
term sections teachers
----------- -------- --------
2025-Autumn 14 11
2025-Spring 16 12
2026-Spring 11 10
(3 rows)
Notice that COUNT(*) and COUNT(DISTINCT ...) give different answers for the same group: the first counts rows, the second counts different values.
4. Counting items that are linked
When one row can appear several times, a plain count may count the same thing more than once. Compare "how many order lines" with "how many different orders":
SELECT COUNT(*) AS order_lines, COUNT(DISTINCT order_id) AS different_orders, COUNT(DISTINCT product_id) AS different_products
FROM order_items;
order_lines different_orders different_products
----------- ---------------- ------------------
5790 2600 45
(1 row)
SELECT COUNT(*) AS ledger_rows, COUNT(DISTINCT account_id) AS different_accounts
FROM transactions;
ledger_rows different_accounts
----------- ------------------
7415 450
(1 row)
SELECT COUNT(*) AS enrollments, COUNT(DISTINCT student_id) AS different_students, COUNT(DISTINCT section_id) AS different_sections
FROM enrollments;
enrollments different_students different_sections
----------- ------------------ ------------------
1002 340 41
(1 row)
Always ask: am I counting rows, or things that may repeat across rows? The second needs DISTINCT.
5. A count that is zero
If no row matches, COUNT returns 0 (not NULL). A GROUP BY simply has no row for a group that does not exist, so it will not show a zero for it. The chapter on joins shows how to list groups with a count of zero.
SELECT COUNT(*) AS rows_found FROM sqlite_master WHERE name = 'no_such_table';
rows_found
----------
0
(1 row)
SELECT COUNT(*) AS rows_found FROM sqlite_master WHERE name = 'no_such_table';
rows_found
----------
0
(1 row)
SELECT COUNT(*) AS rows_found FROM sqlite_master WHERE name = 'no_such_table';
rows_found
----------
0
(1 row)
Worked example: three questions, three counts
A table orders(order_id, customer_id, coupon) has some orders without a coupon.
| Question | Query |
|---|---|
| how many orders? | COUNT(*) |
| how many orders used a coupon? | COUNT(coupon) |
| how many different customers ordered? | COUNT(DISTINCT customer_id) |
| how many orders had no coupon? | COUNT(*) - COUNT(coupon) |
Each question needs a different form. The table is made up.
Common mistakes
- Using
COUNT(column)when you meant all rows. It skipsNULLs. - Counting rows when you meant different things. Add
DISTINCT. - Expecting
GROUP BYto show groups with a count of zero. - Counting a joined result (chapter 4) without thinking about repeats.
Key takeaways
COUNT(*)counts rows;COUNT(column)skipsNULLs;COUNT(DISTINCT column)counts different values.COUNT(*) - COUNT(column)is the number ofNULLs.DISTINCTinside a group answers "how many different ... per group?".- No matching rows gives
0.
Practice
1. Which form counts the rows where phone has a value?
Show answer
COUNT(phone).
2. How do you count the different cities in customers?
Show answer
SELECT COUNT(DISTINCT city) FROM customers;
3. If a table has 100 rows and COUNT(email) is 90, how many rows have no email?
Show answer
10, because COUNT(*) - COUNT(email) is 100 - 90.
4. What does COUNT(DISTINCT x) do with NULL values?
Show answer
It ignores them: NULL is not counted as a value.
5. Why can COUNT(*) be larger than COUNT(DISTINCT customer_id) in an orders table?
Show answer
A customer can place several orders, so the same customer id appears in many rows.
6. What does COUNT(*) return when no row matches?
Show answer
0.