SQL COUNT: COUNT(*), COUNT(column) and COUNT(DISTINCT)
COUNT(*) counts rows. COUNT(column) counts rows where that column is not NULL. COUNT(DISTINCT column) counts different non-NULL values. Interview questions often hinge on exactly this difference.
How do the three differ?
For an orders table with 10 rows, 2 of them without a coupon, and 7 different customers:
| Query | Result |
|---|---|
COUNT(*) | 10 |
COUNT(coupon) | 8 |
COUNT(DISTINCT customer_id) | 7 |
How do you count with a condition?
Filter in WHERE:
SELECT COUNT(*) FROM orders WHERE status = 'paid';
Or count several conditions in one query:
SELECT SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid, SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) AS openFROM orders;
How do you count per group?
SELECT country, COUNT(*) AS customersFROM customersGROUP BY country;
The LEFT JOIN trap
After a LEFT JOIN, COUNT(*) counts a customer without orders as 1. Count a column from the right table instead, COUNT(o.id), to get 0.
Practise COUNT
Updated November 2026.