Skip to content
Codewilds

Lesson 10 of 30 · Aggregates & Grouping

SQL GROUP BY: Count, Sum and Average per Group

medium20 XP2 min read

An aggregate over a whole table answers one question: how much gold do all heroes own? Usually you want the same answer per group — gold per class, quests per region, items per type. GROUP BY splits the rows into groups and runs the aggregates once for every group.

One row per group

SELECT class, SUM(gold) AS total_gold
FROM heroes
GROUP BY class;
class total_gold
Healer 755
Mage 1410
Ranger 1200
Rogue 1565
Warrior 1780

Here is what happens: the database collects all rows with the same class into a group, runs SUM(gold) separately for each group, and returns one row per group. Fifteen heroes become five rows.

Because each group becomes a single row, the SELECT may only contain the grouped columns and aggregates. A column like name has three different values in every class, so there is no single name to show.

Grouping by several columns

List several columns to make a group for every combination of values:

SELECT guild_id, class, COUNT(*) AS heroes
FROM heroes
WHERE guild_id IS NOT NULL
GROUP BY guild_id, class;
guild_id class heroes
1 Healer 1
1 Ranger 2
2 Healer 1
2 Warrior 2
… … …

Guild 1 has one Healer and two Rangers. Note the WHERE: it runs before grouping, so the heroes without a guild never reach a group. Without it, NULL would form a group of its own.

Sorting the groups

ORDER BY can sort by an aggregate, or by its alias:

SELECT class, SUM(gold) AS total_gold
FROM heroes
GROUP BY class
ORDER BY total_gold DESC;

Now the richest class, Warrior with 1780 gold, comes first. Without ORDER BY, groups come back in whatever order the database finds convenient. The clauses go in this order: SELECT … FROM … WHERE … GROUP BY … ORDER BY … LIMIT.

Common mistakes

  • Selecting a column that isn't grouped. SELECT class, name, COUNT(*) FROM heroes GROUP BY class — most databases reject this. SQLite runs it and shows one arbitrary hero per class, which looks right but means nothing.
  • Forgetting GROUP BY. SELECT class, COUNT(*) FROM heroes returns one row with a count of 15, not a count per class.
  • Grouping by the wrong column. GROUP BY name makes one group per hero, so every count is 1.
  • Expecting sorted groups without ORDER BY.

Assignment

The market keeper wants an overview of the items table per type:

id name type price rarity
1 Oak Shortbow weapon 120 common
5 Leather Vest armor 80 common
8 Healing Potion potion 25 common
… … … … …

Return one row per item type with these columns, in this order:

  1. type
  2. items — how many items of that type there are
  3. cheapest — the lowest price of that type
  4. priciest — the highest price of that type

Sort the rows by items, the most items first.

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+20 XP

Lesson 10 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.