2. Summarising data: totals and groups
2.4 Three kinds of COUNT
Highlights & notes
Select lesson text to highlight it, or tap Add note.
Bookmarks
Bookmark a lesson to see it here.
"How many?" sounds like one question, but it is three:
- COUNT(*): how many rows
- COUNT(amount): how many rows where amount is not empty
- COUNT(DISTINCT product): how many different products
Mixing them up is one of the most common mistakes in reports. "We have 8 customers" and "we have 8 orders" are very different statements.
COUNT(column) silently skips NULL. If a count looks too small, check whether the column has missing values.
Rows, and rows with a value
Order 108 has no amount.
SQL
SELECT COUNT(*) AS all_rows, COUNT(amount) AS with_amountFROM orders;
Rows at this step
all_rows | with_amount ---------+------------ 8 | 7
Quick check
A table has 10 rows. The column email is NULL in 3 of them. What is COUNT(email)?
How many different?
Eight orders, but fewer buyers.
SQL
SELECT COUNT(DISTINCT customer_id) AS buyers, COUNT(DISTINCT product) AS productsFROM orders;
Rows at this step
buyers | products -------+--------- 4 | 4
Quick check
Which one answers "how many customers have placed an order"?
Practice in SQL ✓ Solved
Count how many different cities are in customers. Name the column cities.
Tables you can query: customers, orders
customers id | name | city ---+-------+------ 1 | Asha | Noida 2 | Ravi | Delhi 3 | Meera | Pune 4 | Kabir | Noida 5 | Zoya | NULL orders id | customer_id | product | amount | status | order_date ----+-------------+----------+--------+-----------+----------- 101 | 1 | Laptop | 55000 | delivered | 2026-01-05 102 | 2 | Mouse | 700 | delivered | 2026-01-06 103 | 1 | Keyboard | 1500 | delivered | 2026-01-09 104 | 3 | Monitor | 12000 | cancelled | 2026-01-12 105 | 2 | Laptop | 52000 | delivered | 2026-01-15 106 | 4 | Mouse | 650 | pending | 2026-01-18 107 | 1 | Monitor | 11500 | delivered | 2026-01-20 108 | 3 | Keyboard | NULL | pending | 2026-01-22
Expected output
cities ------ 3
Reference solution
SELECT COUNT(DISTINCT city) AS cities
FROM customers;
Question 1 · Easy
What does COUNT(*) count?
Question 2 · Medium
orders has 8 rows and one NULL amount. What is COUNT(amount)?
Question 3 · Hard
A report says "8 customers bought this month" using COUNT(*) on orders. What is wrong?
Key takeaways
COUNT(*) counts rows, COUNT(column) counts rows that have a value, and COUNT(DISTINCT column) counts different values.
Save your progress
Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.
Start free Log in