Students use GROUP BY to get a summary for each value of a column, and sort the groups.
Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 8 min · Investigate 8 min · Modify and Make 14 min
On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.
Give pairs printed song cards. They sort them into genre piles and count each pile, then run the query to check.
Seeing why the count is for a pile, not the table. Sort cards into piles by hand first.
| What you will see | A question to ask | The fix |
|---|---|---|
| Only one row, with an odd genre. | Is there a GROUP BY? | Add GROUP BY and the column you want a row for. |
| GROUP BY comes before WHERE and gives an error. | What is the order of the parts? | SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT. |
One possible solution for each task. Other correct programs are fine.
SELECT genre, SUM(plays)
FROM songs
GROUP BY genre;
SELECT artist, COUNT(*)
FROM songs
GROUP BY artist
ORDER BY COUNT(*) DESC
LIMIT 1;
Stretch: Add WHERE year >= 2020 and see how the counts change.
Support: Give the query with GROUP BY filled in and ask for the summary.
What does GROUP BY genre do?
It makes one pile for each genre, so a summary such as COUNT(*) gives one answer for each genre.
Next lesson: a second table, and joining the two.