Lessons · SQL · COUNT, SUM, AVG
One number from many rows
COUNT, SUM, AVG, MIN, MAX collapse many rows into one value.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Totals, averages and counts are the numbers on every dashboard.
How to think about it
What single number do I want? Decide the single number you want, pick the function, and remember WHERE still applies first: SUM of paid orders means WHERE status = 'paid' then SUM.
Worked example
SELECT COUNT(*), AVG(minutes)Two aggregates at once.
FROM filmsWhere the rows live.
WHERE year >= 2020;Filtered first, then collapsed.
Your turn
Longest film length.
SELECT (minutes) FROM films;
Run a query against real tables
The trap
Mixing an aggregate with a plain column without GROUP BY. Which title goes with the average? The database cannot know.
Practise COUNT, SUM, AVG on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.