Skip to content
Codewilds

Lesson 11 of 30 · Aggregates & Grouping

SQL HAVING vs WHERE: Filter Groups After GROUP BY

medium20 XP2 min read

GROUP BY gives you one row per group, but often you only want some of the groups: the regions with at least two quests, the classes that own more than 1000 gold. WHERE can't do this, because it runs before the groups even exist. That's the job of HAVING.

Filtering groups with HAVING

HAVING comes right after GROUP BY and keeps only the groups whose condition is true. Unlike WHERE, it can use aggregates:

SELECT region, COUNT(*) AS quests
FROM quests
GROUP BY region
HAVING COUNT(*) >= 2;
region quests
Dunewatch 2
Eldermoor 3
Sunreach 2

The other five regions have only one quest each, so their groups are dropped. The aggregate in HAVING doesn't have to appear in the SELECT — HAVING AVG(reward) > 100 works too.

WHERE vs HAVING

WHERE HAVING
runs before grouping after grouping
filters single rows whole groups
can use COUNT, SUM, AVG … no yes

A query is processed in this order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. So WHERE decides which rows go into the groups, and HAVING decides which groups come out.

Using both

Many reports need both filters. This one counts only the veterans (level 15 or higher) and then keeps the classes that have at least two of them:

SELECT class, COUNT(*) AS veterans, SUM(gold) AS total_gold
FROM heroes
WHERE level >= 15
GROUP BY class
HAVING COUNT(*) >= 2;
class veterans total_gold
Mage 2 1390
Warrior 2 1740

Note that total_gold only includes the veterans: Kira Frostvale (a level 3 Mage) and Finn Oakheart (a level 5 Warrior) were removed by WHERE before any sums were calculated.

Common mistakes

  • An aggregate in WHERE. WHERE COUNT(*) >= 2 fails with misuse of aggregate: COUNT(). Conditions on COUNT, SUM, AVG, MIN and MAX go in HAVING.
  • A row condition in HAVING. HAVING difficulty <> 'easy' doesn't remove the easy quests: SQLite checks one arbitrary row of each group, so whole groups can vanish (most other databases reject it). Row conditions go in WHERE.
  • The wrong clause order. HAVING must come after GROUP BY. Writing it before is a syntax error.

Assignment

Which quest regions are worth the trip? The quests table has a region, a difficulty (easy, medium or hard) and a reward:

id title region difficulty reward
2 The Lost Caravan Dunewatch medium 180
6 The Bandit King Dunewatch hard 500
7 Herbs for the Healer Sunreach easy 40
… … … … …

Write a query that:

  1. counts only medium and hard quests — easy quests don't count at all,
  2. groups them by region,
  3. keeps only the regions whose total reward is more than 300,
  4. sorts the regions by their total reward, highest first.

Return the columns region, quests (the number of medium and hard quests) and total_reward (the sum of their rewards), in this order.

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 11 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.