Lesson 9 of 30 · Aggregates & Grouping
SQL SUM, AVG, MIN and MAX: Totals, Averages and ROUND
COUNT tells you how many rows there are. The other aggregate functions tell you about the values in those rows: how much gold all heroes own together, what the average level is, which item is the cheapest. Each one turns a whole column into a single number.
SUM and AVG
SUM adds up a column and AVG calculates its average:
SELECT SUM(gold) AS total_gold, AVG(gold) AS avg_gold FROM heroes;
| total_gold | avg_gold |
|---|---|
| 6710 | 447.3333333333333 |
The heroes own 6710 gold together, 447.33 each on average. AVG always returns a decimal number, even when every value is a whole number.
MIN and MAX
MIN and MAX return the smallest and the largest value:
SELECT MIN(level) AS lowest, MAX(level) AS highest FROM heroes;
| lowest | highest |
|---|---|
| 3 | 23 |
They work on text and dates too: MIN(name) is the name that comes first alphabetically, and MIN(joined) is the earliest join date, 2024-05-09.
Rounding with ROUND
ROUND(value, digits) rounds a number to the given number of decimal places. Without digits, it rounds to a whole number:
SELECT ROUND(AVG(gold), 1) AS one_decimal, ROUND(AVG(gold)) AS whole
FROM heroes;
| one_decimal | whole |
|---|---|
| 447.3 | 447 |
Combining aggregates with WHERE
A SELECT can hold several aggregates at once, and WHERE decides which rows they see. This sums up the prices of the potions in the market:
SELECT COUNT(*) AS potions, SUM(price) AS potion_shelf
FROM items
WHERE type = 'potion';
| potions | potion_shelf |
|---|---|
| 3 | 355 |
All aggregates ignore NULL values: AVG of 10, NULL and 20 is 15, not 10.
Common mistakes
- Averaging by hand.
SUM(gold) / COUNT(*)gives447, not447.33— dividing two whole numbers drops the decimals. UseAVG(gold). - Rounding too early.
AVG(ROUND(gold, 1))rounds every single value and then averages them, so the result still has many decimals. Round the final result:ROUND(AVG(gold), 1). - Expecting 0 from an empty sum. When
WHEREmatches no rows,SUM,AVG,MINandMAXreturn NULL, not 0. OnlyCOUNTreturns 0.
Assignment
The quest board wants statistics about the quests outside Eldermoor. The quests table looks like this:
| id | title | region | difficulty | reward |
|---|---|---|---|---|
| 1 | Rats in the Cellar | Eldermoor | easy | 50 |
| 2 | The Lost Caravan | Dunewatch | medium | 180 |
| 10 | Dragon Sighting | Sunreach | hard | 800 |
| … | … | … | … | … |
Write one query that returns a single row with these columns, in this order, counting only quests whose region is not 'Eldermoor':
quests— how many quests there aretotal_reward— the sum of their rewardsavg_reward— the average reward, rounded to 1 decimal placemin_reward— the smallest rewardmax_reward— the largest reward
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 .