← SQL and databases

One answer for each group

Use GROUP BY to count or add up for each genre, artist or year at once.

How many songs in each genre? You could run five queries, or ask once.

COUNT(*) gave one answer for the whole table. GROUP BY sorts the rows into piles, one pile for each value in a column, and gives one answer for each pile.

How this lesson works: Predict, Run, Investigate, Modify, Make
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 genre, COUNT(*)
FROM songs
GROUP BY genre;
There are five different genres. What will this show?

RunRun it and compare the result with your prediction.

Run

Run it and add up the counts in your head. They should come to 15.

Part What it does
SELECT genre, COUNT(*) Shows the group's name and its count
GROUP BY genre Makes one pile for each genre
lesson.sql
SELECT genre, COUNT(*)
FROM songs
GROUP BY genre;
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.sql
SELECT artist, AVG(seconds)
FROM songs
GROUP BY artist;

One row for each pile.

Group by the column you want a row for.

Run the query. How many different artists are in the table?

You want the total plays for each artist. Which query?

ModifyChange the code on purpose and check the result.

Modify

Change the query so it shows the total plays for each genre.

Try it yourselfYour edits are saved in this browser.
Build up to it
Which query gives the average length for each genre?

Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

Change COUNT(*) to SUM(plays).

Another hint

SELECT genre, SUM(plays) FROM songs GROUP BY genre;

Show the total plays for each genre.

    Run your program. This ticks when your code does all of the above.

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Find out which artist has the most songs in the library. Group, count, sort and keep the top one.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Before a hint: what kind of stuck is this? Try a strategy first

    Group by artist or year, then ORDER BY the summary.

    Another hint

    Add ORDER BY COUNT(*) DESC after GROUP BY.

    Write a grouped query that is sorted with ORDER BY.

      Run your program. This ticks when your code does all of the above.

      Now you can
      Next lesson6. Join two tablesCombine rows from two tables with JOIN ... ON, matching a column they share.
      For teachers

      Students use GROUP BY to get a summary for each value of a column, and sort the groups. About 30 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Running the lesson

      1. Predict: draw five piles on the board and sort a few song cards into them before anyone runs it.
      2. Run: the counts add to 15, which shows no song is lost or counted twice.
      3. Make: ORDER BY can use the summary, as in ORDER BY COUNT(*) DESC.

      The hard part (on purpose)

      Seeing why the count is for a pile, not the table. Sort cards into piles by hand first.

      When students get stuck

      Stretch and support

      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.

      Where it leads

      Next lesson: a second table, and joining the two.

      What each step is for