← SQL and databases

Count and add up

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
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';
Four songs are pop. What will this query show?

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?

Which query gives the length of the longest song?

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.
Build up to it
Which query counts the songs from 2022?

Need a hint?

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

Keep COUNT(*) and change the test in WHERE.

Another hint

You want WHERE artist = 'Kito'.

Count the songs by Kito.

    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

    Ask the library a question that has one number as its answer, such as the average length of a pop song. Use SUM, AVG or MAX.

    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

    Pick one of SUM, AVG or MAX, and one column.

    Another hint

    SELECT AVG(seconds) FROM songs WHERE genre = 'folk';

    Write a query that uses SUM, AVG or MAX and gives one row.

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

      Now you can
      Next lesson5. One answer for each groupUse GROUP BY to count or add up for each genre, artist or year at once.
      For teachers

      Students summarise many rows into one value with COUNT, SUM, AVG and MAX, filtered by WHERE. About 25 minutes.

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

      Students will be able to

      Running the lesson

      1. Predict: the four-rows option is the main slip, expecting a list. Ask how many values a count can be.
      2. Run: take off WHERE and run again so students see 15.
      3. 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

      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