Turn many rows into one answer with COUNT, SUM, AVG and MAX.
How many songs, how many plays, how long on average: the database can answer without showing you a single song.
So far a query gave back rows from the table. A summary function turns many rows into one value: COUNT(*) counts rows, SUM(plays) adds a column up, AVG averages it and MAX finds the biggest.
How this lesson works: Predict, Run, Investigate, Modify, Make
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
lesson.sql
SELECT COUNT(*)
FROM songs
WHERE genre = 'pop';
RunRun it and compare the result with your prediction.
Run
Run it, then take off the WHERE line and run it again.
Part
What it does
COUNT(*)
Counts rows. The star means "whole rows"
WHERE genre = 'pop'
Filters first, so only pop rows are counted
lesson.sql
SELECT COUNT(*)
FROM songs
WHERE genre = 'pop';
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.
Investigate
lesson.sql
SELECT SUM(plays)
FROM songs
WHERE genre = 'rock';
SUM works on the rows that pass WHERE.
Each function answers a different question.
Run the query. How many plays do the rock songs have in total?
ModifyChange the code on purpose and check the result.
Modify
Change the query so it counts the songs by Kito.
Try it yourselfYour edits are saved in this browser.
I can add up, average or find the biggest value in a column.
I can use WHERE to choose which rows are summarised.
Running the lesson
Predict: the four-rows option is the main slip, expecting a list. Ask how many values a count can be.
Run: take off WHERE and run again so students see 15.
Investigate: MAX, SUM and COUNT answer different questions. Read each aloud as a question.
The hard part (on purpose)
Expecting a list back. Ask how many values a count can be.
When students get stuck
A column name and COUNT(*) together give an odd result. Ask: Is the answer one value or a list? Fix: Summaries give one row. Mixing in a plain column needs GROUP BY, which is the next lesson.
COUNT(*) shows 15 when it should show fewer. Ask: Did the WHERE line stay? Fix: Add the WHERE line back before the semicolon.
Stretch and support
Stretch: Show the count and the average length in one query.
Support: Give the whole query with the function name missing.
Where it leads
Next lesson: one count for each genre at once, with GROUP BY.
What each step is for
Investigate: A summary function squashes the rows that pass WHERE into one value. WHERE decides which rows go in.