← Teacher guide

Count and add up

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

SQL and databases · Summarise · about 25 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

Timing

Predict and Run 8 min · Investigate 7 min · Modify and Make 10 min

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.

In class

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.

The hard part (on purpose)

Expecting a list back. Ask how many values a count can be.

When students get stuck

What you will seeA question to askThe fix
A column name and COUNT(*) together give an odd result.Is the answer one value or a list?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.Did the WHERE line stay?Add the WHERE line back before the semicolon.

Answers to the checks

  1. Predict: Four songs are pop. What will this query show?
    One row with the number 4. COUNT(*) counts the rows that pass WHERE, and the answer is a single value.
  2. Add them up: Run the query. How many plays do the rock songs have in total?
    7900
  3. Pick the function: Which query gives the length of the longest song?
    SELECT MAX(seconds) FROM songs;
    MAX(column) gives the biggest value in the column.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

SELECT COUNT(*)
FROM songs
WHERE artist = 'Kito';

Make

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

Stretch and support

Stretch: Show the count and the average length in one query.

Support: Give the whole query with the function name missing.

Exit ticket

What is the difference between SELECT title and SELECT COUNT(*)?

SELECT title gives one row for each song. COUNT(*) gives one row with how many rows there are.

What each step is for

Where it leads

Next lesson: one count for each genre at once, with GROUP BY.