Lesson 24 of 30
80%
GROUP BY and HAVING
GROUP BY makes piles. Then aggregates run on each pile.
HAVING filters piles. WHERE filters rows before the piles.
Count per city
sql
Result
| city | how_many |
|---|---|
| London | 1 |
| Kenya | 1 |
| Taipei | 1 |
| Cairo | 1 |
| Unknown | 1 |
5 rows
Average score per student
sql
Result
| name | avg_points |
|---|---|
| Ada | 91.5000 |
| Sam | 90.0000 |
2 rows
HAVING
WHERE cannot see AVG. HAVING can.
sql
Result
| city | how_many |
|---|
Empty set
The usual order
1
FROM and JOIN
2
WHERE
3
GROUP BY
4
HAVING
5
ORDER BY
6
LIMIT
Tip: Every selected column that is not an aggregate must appear in GROUP BY.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.