🎯 Syllabus & Goals 3 min
Cambridge 9.1 · SQL scripts to query data — SUM, COUNT Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Use
SUMto total a numeric field, with or without aWHEREcondition. - Use
COUNTto count the records that match a condition. - Decide whether a question needs
SUMorCOUNT, and state the single value returned.
Textbook: Chapter 9, §9.1.4 (pp. 344–348)
Recap / Warm-Up 5 min
Last lesson you filtered with AND, OR, NOT, BETWEEN, IN and LIKE. Here is FILM once more.
| FilmID | Title | Genre | ReleaseYear | Minutes | Price | Copies |
|---|---|---|---|---|---|---|
| F01 | Ocean Deep | Documentary | 2021 | 95 | 3.99 | 4 |
| F02 | Robot Rescue | Animation | 2019 | 88 | 2.99 | 6 |
| F03 | The Last Orbit | Sci-Fi | 2023 | 132 | 5.49 | 3 |
| F04 | Laugh Out Loud | Comedy | 2018 | 101 | 1.99 | 5 |
| F05 | Storm Chasers | Action | 2022 | 117 | 4.49 | 2 |
| F06 | Star Academy | Sci-Fi | 2020 | 124 | 3.49 | 5 |
| F07 | Pizza Party | Comedy | 2023 | 92 | 4.99 | 7 |
| F08 | Silent Forest | Drama | 2017 | 110 | 1.49 | 1 |
| F09 | Space Pups | Animation | 2022 | 84 | 3.99 | 8 |
| F10 | Second Chance | Drama | 2021 | 106 | 2.49 | 3 |
Quick starter
Without SQL: how many Animation films are there, and how many copies of them does the shop hold in total?
Reveal the answer
2 films (F02, F09). 6 + 8 = 14 copies. You just did a COUNT and a SUM by hand.
🧠 Key Concept 14 min
1 · Two questions a list cannot answer
Some questions need one number, not a list of records. "How many copies do we hold?" is a total. "How many films cost under 3.00?" is a count.
| Function | What it returns | Form |
|---|---|---|
SUM | The total of all the values in a field. The field must be integer or real. | SELECT SUM(Field) |
COUNT | The number of records (rows) that match the condition. | SELECT COUNT(Field) |
2 · SUM
With no WHERE, SUM totals the whole field — every copy of every film:
SELECT SUM(Copies) FROM FILM;
| SUM(Copies) |
|---|
| 44 |
With a WHERE, only matching records are added. Total running time of Sci-Fi and Action films:
SELECT SUM(Minutes)
FROM FILM
WHERE Genre IN ('Sci-Fi', 'Action');| SUM(Minutes) |
|---|
| 373 |
3 · COUNT
COUNT(Field) counts the records that pass the WHERE test. Counting the primary key is a safe choice, because every record has one. COUNT(*) counts records too:
SELECT COUNT(*) FROM FILM;
| COUNT(*) |
|---|
| 10 |
SUM for the day's takings. A school asks COUNT for the number of students absent today. A warehouse asks both: how many product lines are low, and how many items in total.Worked Example 12 min
(a) "How many copies of Animation films are held?"
- Decide: SUM or COUNT? "How many copies" asks for a total of the
Copiesvalues → SUM.Counting records would give 2 films, not the number of copies. SELECT SUM(Copies)Copies is an integer field, so it can be added.FROM FILMWHERE Genre = 'Animation';- Matching records: F02 (6) and F09 (8). Add: 6 + 8 = 14.
SELECT SUM(Copies) FROM FILM WHERE Genre = 'Animation';
| FilmID | Title | Genre | ReleaseYear | Minutes | Price | Copies |
|---|---|---|---|---|---|---|
| F01 | Ocean Deep | Documentary | 2021 | 95 | 3.99 | 4 |
| F02 | Robot Rescue | Animation | 2019 | 88 | 2.99 | 6 |
| F03 | The Last Orbit | Sci-Fi | 2023 | 132 | 5.49 | 3 |
| F04 | Laugh Out Loud | Comedy | 2018 | 101 | 1.99 | 5 |
| F05 | Storm Chasers | Action | 2022 | 117 | 4.49 | 2 |
| F06 | Star Academy | Sci-Fi | 2020 | 124 | 3.49 | 5 |
| F07 | Pizza Party | Comedy | 2023 | 92 | 4.99 | 7 |
| F08 | Silent Forest | Drama | 2017 | 110 | 1.49 | 1 |
| F09 | Space Pups | Animation | 2022 | 84 | 3.99 | 8 |
| F10 | Second Chance | Drama | 2021 | 106 | 2.49 | 3 |
| SUM(Copies) |
|---|
| 14 |
(b) "How many films cost less than 3.00?"
- Decide: the answer is a number of films (records) → COUNT.
SELECT COUNT(FilmID)FilmID is the primary key, so every record has a value to count.FROM FILMWHERE Price < 3.00;Strictly less than: a film at exactly 3.00 would not count.- Matching records: F02 2.99, F04 1.99, F08 1.49, F10 2.49 → 4.
SELECT COUNT(FilmID) FROM FILM WHERE Price < 3.00;
| COUNT(FilmID) |
|---|
| 4 |
(c) COUNT with BETWEEN
SELECT COUNT(Title) FROM FILM WHERE ReleaseYear BETWEEN 2020 AND 2022;
- Years 2020, 2021 and 2022 all count — BETWEEN includes both ends.
- F01 2021, F05 2022, F06 2020, F09 2022, F10 2021 → 5.F02 (2019) and F03/F07 (2023) are outside.
| COUNT(Title) |
|---|
| 5 |
Try It Yourself 12 min
Tasks use the BAKERY table in Exam Practice below.
Goal: Write a query to find the total number of items sold today across the whole bakery. State the value it returns.
Goal: Write a query to count the items priced from 2.00 to 3.00 inclusive. State the value returned.
Goal: The manager asks "how many pies did we sell?". Write the query, then explain why SELECT COUNT(ItemCode) … WHERE Category = 'Pie' gives the wrong answer.
Hint
COUNT would tell you how many kinds of pie are on the list. The number sold is stored in SoldToday.
📝 Exam Practice 10 min
A bakery records each item and how many were sold today in the table BAKERY.
| ItemCode | ItemName | Category | Price | SoldToday |
|---|---|---|---|---|
| B01 | Apple Pie | Pie | 3.20 | 14 |
| B02 | Banana Bread | Loaf | 2.50 | 9 |
| B03 | Carrot Cake | Cake | 2.80 | 11 |
| B04 | Cherry Pie | Pie | 3.40 | 6 |
| B05 | Choc Brownie | Cake | 1.90 | 22 |
| B06 | Seeded Loaf | Loaf | 2.10 | 8 |
| B07 | Lemon Cake | Cake | 2.60 | 15 |
| B08 | Plum Pie | Pie | 3.00 | 4 |
Show the output from this query.
SELECT SUM(SoldToday) FROM BAKERY WHERE Category = 'Pie';
Mark scheme
- 24 (1) — 14 + 6 + 4.
Show the output from this query.
SELECT COUNT(ItemCode) FROM BAKERY WHERE Price > 2.50 AND SoldToday >= 10;
Mark scheme
- 3 (1) — B01, B03, B07.
Write an SQL query to count the number of items in the Cake category.
Mark scheme
SELECT COUNT(ItemCode) FROM BAKERY WHERE Category = 'Cake';
SELECT COUNT(ItemCode)— accept any field, or*(1).FROM BAKERY(1).WHERE Category = 'Cake'(1).
Returns 3.
Explain the difference between SUM and COUNT, using the BAKERY table.
Mark scheme
- SUM adds up the values in a numeric field, e.g.
SUM(SoldToday)gives the total items sold (1). - COUNT counts the number of records that match a condition, e.g. how many items are pies (1).
Recap & Key Terms 3 min
SUM and COUNT go in the SELECT clause and return one value. SUM adds numbers in a field; COUNT counts matching records. A WHERE clause decides which records take part.
- SUM
- Returns the sum of all the values in a field (column); used with SELECT. The field must be integer or real.
- COUNT
- Counts the number of records (rows) where the field matches a specified condition; used with SELECT.
- Result set
- The data a query returns — here, a single value.
- Condition
- The test in a WHERE clause that decides which records are included.
Homework 1 min
Task (≤ 15 min): Using BAKERY, show the output of this query and write one sentence saying what it tells the owner.
SELECT SUM(SoldToday) FROM BAKERY WHERE Category <> 'Loaf';
Model answer
| SUM(SoldToday) |
|---|
| 72 |
Pies 14 + 6 + 4 = 24; cakes 11 + 22 + 15 = 48; total 72. It is the number of items sold today that were not loaves.