Skip to content
Codewilds

Lesson 8 of 30 · Aggregates & Grouping

SQL COUNT and DISTINCT: Count Rows and Unique Values

easy10 XP2 min read

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. Use COUNT(*) 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 to 15. The next lessons show how to count per group instead.
  • Expecting DISTINCT to work on one column only. It applies to the whole row: SELECT DISTINCT class, name FROM heroes still 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:

  1. heroes — the number of heroes
  2. in_guild — the number of heroes who belong to a guild (their guild_id is not NULL)
  3. 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
Play the 7 free lessons

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 .

Write code ↓
SQL+10 XP

Lesson 8 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.