Lesson 8 of 30 · Aggregates & Grouping
SQL COUNT and DISTINCT: Count Rows and Unique Values
Until now, every row in your results was a row of the table. Aggregate functions work the other way round: they look at many rows and answer with a single value. How many heroes are there? How many different classes? This chapter is about summarising data, and it starts with counting.
Counting rows with COUNT(*)
COUNT(*) counts the rows of the result:
SELECT COUNT(*) AS veterans FROM heroes WHERE level >= 15;
| veterans |
|---|
| 7 |
WHERE runs first and COUNT counts what is left — here, the 7 heroes at level 15 or above. Without the WHERE, the answer is 15, the number of heroes in the table.
COUNT(column) skips NULLs
Inside the brackets you can also put a column. COUNT(column) counts only the rows where that column has a value:
SELECT COUNT(*) AS heroes, COUNT(guild_id) AS with_guild FROM heroes;
| heroes | with_guild |
|---|---|
| 15 | 12 |
Three heroes — Dax Shadowstep, Iris Dawnpetal and Kira Frostvale — have no guild, so their guild_id is NULL and COUNT(guild_id) leaves them out. That makes COUNT(column) a quick way to ask "how many rows have this filled in?".
Removing duplicates with DISTINCT
SELECT DISTINCT removes duplicate rows from a result:
SELECT DISTINCT class FROM heroes;
This returns 5 rows — Ranger, Warrior, Mage, Rogue and Healer — instead of 15. With several columns, DISTINCT removes duplicate combinations: SELECT DISTINCT region, difficulty FROM quests lists Eldermoor | easy once, even though three easy quests are in Eldermoor.
Counting unique values with COUNT(DISTINCT …)
Put DISTINCT inside COUNT to count how many different values a column has. The completions table (hero_id, quest_id, completed_on) has one row for every quest a hero finished:
SELECT COUNT(*) AS completions, COUNT(DISTINCT hero_id) AS heroes
FROM completions;
| completions | heroes |
|---|---|
| 29 | 12 |
29 quests were completed, by 12 different heroes. Like COUNT(column), COUNT(DISTINCT column) ignores NULLs.
Common mistakes
COUNT(column)when you mean "rows".COUNT(guild_id)is 12, not 15. UseCOUNT(*)to count rows.- Mixing an aggregate with a plain column.
SELECT name, COUNT(*) FROM heroes;makes no sense: 15 names but one count. Most databases reject it; SQLite runs it and shows one arbitrary name next to15. The next lessons show how to count per group instead. - Expecting
DISTINCTto work on one column only. It applies to the whole row:SELECT DISTINCT class, name FROM heroesstill returns all 15 heroes, because every combination is different.
Assignment
Write one query on the heroes table that returns a single row with three counts, in this order:
heroes— the number of heroesin_guild— the number of heroes who belong to a guild (theirguild_idis not NULL)guilds— the number of different guilds that have at least one member
Let COUNT do the work — don't type the numbers in yourself.
Premium lesson
Unlock the full Learn SQL course
- All 30 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.
Lesson last updated on .