COUNT collapses many rows into one number. COUNT(*) counts rows; COUNT(column) counts rows where that column is not NULL; COUNT(DISTINCT column) counts unique values. Pair it with WHERE (“how many engineers?”) or GROUP BY (“how many per department?”).
--how it works
1
COUNT collapses rows to one number
Aggregation turns a table into a summary — COUNT is the simplest aggregate.
2
Three spellings, three meanings
COUNT(*) = rows; COUNT(col) = rows where col is not NULL; COUNT(DISTINCT col) = unique non-NULL values.
3
Scope with WHERE first
WHERE department = 'Engineering' narrows the population before counting.
4
Alias the result
SELECT COUNT(*) AS headcount — the output column otherwise gets an engine-specific name.
5
Per-group counting comes next
Add GROUP BY department and you get one count per bucket — the GROUP BY topic continues from here.
--visualization
WHERE department =
COUNT(*)
6
Stage 1 / 8
SELECT COUNT(*) FROM employees — how many rows match?
--example
query.sql
1SELECTCOUNT(*)ASheadcount
2FROMemployees
3WHEREdepartment='Engineering';
Result
headcount
3
Alice, Cynthia and Farid satisfy the filter → 3. The visualization walks the rows one by one as the counter ticks up.
--variants
COUNT(*)
Total rows, NULLs included.
query.sql
1SELECTCOUNT(*)FROMemployees;
Result
COUNT(*)
6
1Six rows exist; six are counted.
2NULL columns change nothing — * means rows, not values.
3The cheapest question a database answers well.
COUNT(column)
Only non-NULL values of that column.
query.sql
1SELECTCOUNT(bonus)FROMemployees;
Result
COUNT(bonus)
3
1Three employees have bonuses; three NULLs are skipped.
2COUNT(*) said 6, COUNT(bonus) says 3 — know which you wrote.
3Same rule powers COUNT(name) etc. on any sparse column.
COUNT(DISTINCT …)
How many different values?
query.sql
1SELECTCOUNT(DISTINCTdepartment)FROMemployees;
Result
COUNT(DISTINCT department)
3
1First the distinct set {Engineering, Sales, Marketing}.
2Then the count of that set.
3The interview-favourite "how many different…?" shape.
Scoped with WHERE
Count a subset.
query.sql
1SELECTCOUNT(*)ASengineers
2FROMemployees
3WHEREdepartment='Engineering';
Result
engineers
3
1WHERE runs before aggregation — the population narrows first.
2Three rows remain; COUNT reports 3.
3Swap the filter and re-run: the visualization counts whatever passes.
--key facts
COUNT(*) vs COUNT(col)the latter skips NULLs — different answers on sparse data
DISTINCTCOUNT(DISTINCT department) = 3 on our sample data
Fast pathsengines answer COUNT(*) from index metadata when possible
--use cases
Badges and totals: unread messages, open orders, active users
Data-quality checks: COUNT(*) vs COUNT(email) exposes gaps
Pagination totals: "1–20 of 347 results"
Grouped headcounts via GROUP BY department
--under the hood
COUNT(*) is metadata-fast when no filter applies; with WHERE it walks the most selective index.
COUNT(DISTINCT …) needs hashing/sorting — on huge tables it is one of the pricier aggregates.
Practice SQL interview questions
Every SQL lesson maps to the matching interview topic, with answers written for current SQL engines.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses →Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free →Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books →