Lesson 11 of 30 · Aggregates & Grouping
SQL HAVING vs WHERE: Filter Groups After GROUP BY
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(*) >= 2fails withmisuse of aggregate: COUNT(). Conditions onCOUNT,SUM,AVG,MINandMAXgo inHAVING. - 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 inWHERE. - The wrong clause order.
HAVINGmust come afterGROUP 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:
- counts only medium and hard quests — easy quests don't count at all,
- groups them by
region, - keeps only the regions whose total reward is more than 300,
- 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
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 .