S
Topic 9 Databases · Page 9.6–9.7

Aggregating Lab

Extract insight from data. SUM adds a numeric field. COUNT counts records. Two functions, one rule: how much? or how many? Get that right and the rest of the query follows.

🧪 Exam Mode ON — recall from memory, then reveal

📚 Topic overview

In the Foundations and Querying Labs you learned how a database is shaped and how to ask it questions. In this lab you learn how to squeeze insight out of the answers. Instead of listing every record, you'll return a single number — a total, or a count.

The syllabus lists two aggregate functions: SUM (adds up a numeric field) and COUNT (counts records). Questions may use one or both. The core skill is knowing which one a question is asking for.

Before you start: you need everything from the Foundations and Querying Labs — the six data types, primary keys, and the four query clauses (SELECT, FROM, WHERE, ORDER BY). If any of that feels shaky, jump back for five minutes.

🎯 Learning objectives

Cambridge 0478 Topic 9 · outcome 4 (SUM and COUNT)
Read, complete and write SQL scripts that use SUM and COUNT to query a single table, with or without WHERE (and AND / OR).
Identify the output of a SUM or COUNT query — the single value it returns.
By the end of this lab you can
Choose SUM for "how much?" (a total) and COUNT for "how many?" (a number of records).
Write SELECT SUM(FieldName) FROM Table; — knowing SUM only adds up Integer or Real fields.
Write SELECT COUNT(*) FROM Table; to count records.
Add a WHERE clause so only matching records are totalled or counted.
Explain what a given SUM or COUNT statement does.

📖 Key terminology

TermMeaning
Aggregate functionA function that turns many records into one value. The syllabus uses SUM and COUNT.
SUM(FieldName)Adds up the values of a numeric field across all matching records. Returns one number.
COUNT(*)Counts the matching records. Returns one whole number.
Numeric fieldA field whose data type is Integer or Real. Only numeric fields can be summed.
"How much?"The cue for SUM: total cost, total population, total minutes.
"How many?"The cue for COUNT: number of books, number of drinks, number of capital cities.
Filter, then totalWHERE chooses the records; SUM or COUNT then reduces them to one number.

SQL keywords such as SUM and COUNT are usually written in capitals so they stand out from field names.

🧠 Core theory

1. The one question to ask first

Before you write a single character of SQL, read the question and ask yourself:

🗝️ How much? or How many?

How much → SUM. Adding up quantities. Total cost. Total population. Total minutes.
How many → COUNT. Counting things. Number of books. Number of drinks. Number of capital cities.

Get this right and the rest of the query follows. Choose the wrong function and the query answers a different question.

2. SUM — adding up a numeric field

Syntax:

SELECT SUM(FieldName)
FROM Table
WHERE Condition;                  (optional)
  • Result: a single number — the total of that field across all matching records.
  • Field must be Integer or Real. Adding up a Text, Boolean or Character field has no meaning — choose the numeric field the question is about.
  • WHERE is optional — leave it out to sum every record; include it to sum only the records that match a condition.

Example prompt: "The Contract table has a Months field. Write SQL to return the total number of months across all contracts." → SELECT SUM(Months) FROM Contract;

3. COUNT — counting records

SELECT COUNT(*)
FROM Table
WHERE Condition;                  (optional)
  • Result: one whole number — how many records match.
  • COUNT(*) counts records, so it doesn't matter what is in the fields.
  • You may also see COUNT(FieldName), e.g. COUNT(Title). On the tables in this lab it gives the same answer.

📎 Useful background

In real databases, a field can be left empty (a null value). COUNT(*) counts every matching record; COUNT(FieldName) only counts records where that field is not empty. The exam tables you'll see normally have every field filled in.

4. Combining with WHERE

The WHERE clause runs first: it selects the records to look at. Then SUM or COUNT reduces those records to a single number.

➕

SELECT SUM(Price) FROM StorageUnit WHERE Available = TRUE;
"Total price of the available units." WHERE filters; SUM totals.

🔢

SELECT COUNT(*) FROM MajorCity WHERE Capital = TRUE;
"Number of capital cities." WHERE filters; COUNT tallies.

Quoting carries over from the Querying Lab. Text values in WHERE quoted (= "Asia"). Booleans unquoted (= TRUE). Numbers unquoted (> 5).

5. What SUM and COUNT never do

  • Return a list of records. The result is always one number.
  • Take multiple fields. SUM(Price, Weight) is invalid — one field at a time.
  • Need ORDER BY. There's nothing to sort — you get one number.
  • Show which records were counted. If a question asks "list the records" as well, that's a separate SELECT.

6. Explain-the-purpose questions

A question may give you an SQL statement and ask you to explain its purpose. A clear answer covers three things:

  1. What it does — totals a field / counts records.
  2. Which field — e.g. the Minutes field (for SUM).
  3. Which records — e.g. every contract or only songs where the Genre is rock.

7. What does it return?

You may also be asked for the value a query returns. Work it out in two steps:

  1. Use the WHERE clause to pick out the matching records.
  2. COUNT → count them. SUM → add up the field for those records only.

Write just the number — no working, no units, no punctuation.

✏️ Worked examples

The examples use one SoftDrinks sample table, in the style of a recent Paper 2 question.

The SoftDrinks sample table

DrinkID  DrinkName     Supplier             Container  SizeCl  NumberInStock  ReorderLevel  Reordered
D01      Cola          Cambridge Beverages  Can        33      30             15            TRUE
D02      Orange Fizz   Cambridge Beverages  Bottle     50      12             10            FALSE
D03      Lemon Splash  Fenland Drinks       Can        33      45             20            FALSE
D04      Ginger Zing   Cambridge Beverages  Can        33      8              15            TRUE
D05      Apple Crush   Fenland Drinks       Bottle     50      22             10            FALSE
D06      Berry Blast   Fenland Drinks       Can        50      18             15            FALSE
D07      Mint Cooler   Cambridge Beverages  Bottle     33      15             10            TRUE

Reordered is a Boolean field (TRUE/FALSE).

Example 1 · SUM every value in a numeric field

SELECT SUM(NumberInStock)
FROM SoftDrinks;

Returns: 150 (30 + 12 + 45 + 8 + 22 + 18 + 15).

Teaching point: no WHERE = every record is included in the total.

Example 2 · SUM with a WHERE

SELECT SUM(NumberInStock)
FROM SoftDrinks
WHERE Container = "Can";

Returns: 101 (30 + 45 + 8 + 18 — the four drinks in cans).

Teaching point: "Can" is text, so it is in quotes. The 2024 s24 examiner report noted answers that missed these quotes.

Example 3 · COUNT every record

SELECT COUNT(*)
FROM SoftDrinks;

Returns: 7 (seven drinks in the table).

Teaching point: COUNT(*) counts records — it doesn't look at what's inside the fields.

Example 4 · COUNT with a WHERE

SELECT COUNT(*)
FROM SoftDrinks
WHERE Supplier = "Cambridge Beverages";

Returns: 4 (Cola, Orange Fizz, Ginger Zing, Mint Cooler).

Teaching point: "How many" = COUNT. We're counting records, not adding values.

Example 5 · COUNT with a Boolean WHERE

SELECT COUNT(*)
FROM SoftDrinks
WHERE Reordered = TRUE;

Returns: 3 (Cola, Ginger Zing, Mint Cooler).

Teaching point: Reordered is Boolean, so TRUE has no quotes. Always check the data type of the field.

Example 6 · SUM vs COUNT — the classic contrast

SELECT SUM(NumberInStock) FROM SoftDrinks WHERE Container = "Can";
-- Returns 101 (total stock across all Can drinks)

SELECT COUNT(*)            FROM SoftDrinks WHERE Container = "Can";
-- Returns 4 (number of records that are Cans)

Teaching point: same WHERE, completely different questions. SUM asks "how much stock in total?"; COUNT asks "how many kinds of Can drink?".

Example 7 · Explain the purpose

SELECT SUM(Minutes) FROM Songs WHERE Genre = "rock";

Model answer: This statement totals the length in minutes of every song from the Songs table where the Genre is "rock". It returns a single number — the total duration of all rock songs.

Teaching point: three parts — what it does · which field · which records.

Example 8 · Explain a COUNT (same Songs table)

SELECT COUNT(Title) FROM Songs WHERE Genre = "rock";

Model answer: This statement counts the number of songs in the Songs table where the Genre is "rock". It returns a single whole number — the number of rock song titles recorded.

Teaching point: COUNT(Title) counts records with a non-null Title. On this table it gives the same answer as COUNT(*).

⚠️ Common misconceptions

Trap 1 · Using COUNT when the question wants SUM

❌ Question: "What is the total stock of drinks in cans?"
❌ Wrong answer: SELECT COUNT(NumberInStock) FROM SoftDrinks WHERE Container = "Can";
Why wrong: COUNT gives 4 (the number of Can drinks), not the total stock (101). "Total" means SUM.

Cited: 2024 s24 P2 Q9(b) examiner report — only a minority used SUM correctly.

Trap 2 · Using SUM when the question wants COUNT

❌ Question: "How many drinks are supplied by Cambridge Beverages?"
❌ Wrong answer: SELECT SUM(Supplier) FROM SoftDrinks WHERE Supplier = "Cambridge Beverages";
Why wrong: "How many" means COUNT — and Supplier is text, so it can't be added up anyway.

Common exam mistake

Trap 3 · SUM on a non-numeric field

❌ Writing SELECT SUM(Container) FROM SoftDrinks;

SUM adds up Integer and Real fields only. Adding up text or TRUE/FALSE values has no meaning — choose the numeric field the question is about.

Common exam mistake

Trap 4 · Forgetting the WHERE when the question filters

❌ Question: "Total stock of drinks that are in cans"
❌ Wrong answer: SELECT SUM(NumberInStock) FROM SoftDrinks;
Why wrong: this totals every drink (150). "In cans" is a filter, so WHERE Container = "Can" is needed.

Cited: 2024 s24 P2 Q9(b) — the WHERE condition was part of the answer.

Trap 5 · Missing quotes on text values in WHERE

❌ Writing WHERE Container = Can instead of WHERE Container = "Can"

The quoting rule from the Querying Lab still applies. Text values go in quotation marks.

Cited: 2024 s24 P2 Q9(b) examiner report — many answers missed the quotation marks around Can.

Trap 6 · Quoting Booleans or numbers in WHERE

❌ Writing WHERE Available = "TRUE" or WHERE Price > "5"

Numbers and TRUE/FALSE are not quoted — that doesn't change when you use SUM or COUNT.

Common exam mistake

Trap 7 · Two fields inside one SUM or COUNT

❌ Writing SELECT SUM(Price, SizeM3) FROM StorageUnit;

SUM and COUNT take one field. Two totals need two separate queries.

Common exam mistake

Trap 8 · Expecting a list of records back

❌ Thinking SELECT SUM(Price) FROM StorageUnit; shows each price as well as the total

SUM and COUNT return one number. If a question also wants the records listed, that's a separate SELECT query.

Common exam mistake

Trap 9 · Adding ORDER BY to SUM or COUNT

❌ Writing SELECT SUM(Months) FROM Contract ORDER BY Months;

One number has nothing to sort, so ORDER BY isn't needed. Leave it out.

Common exam mistake

Trap 10 · Vague "explain the purpose" answers

❌ Writing "it finds the songs" for SELECT SUM(Minutes) FROM Songs WHERE Genre = "rock";

Say what it does (totals / counts), which field, and which records: "It totals the Minutes of all the rock songs."

Common exam mistake

🎯 Exam focus

Habits that keep marks safe.

"How much?" = SUM. "How many?" = COUNT. Decide this before writing any SQL.
SUM adds up numbers only. Integer and Real fields — never Text, Character or Boolean.
COUNT counts records. COUNT(*) is the simplest form.
One field in the brackets, one number out. SUM(Price, Weight) is not valid.
WHERE first. It picks the records; SUM or COUNT then reduces them to one number.
Quotes still matter. Text in quotes; numbers and TRUE/FALSE without.
No ORDER BY needed. One number has nothing to sort.
Explain = what · which field · which records.
Write SUM and COUNT in capitals so they stand out from field names, and end with ;.

🧪 Quick knowledge check

Click each question to reveal the answer. 0 / 9 revealed

1. Which single question should you ask before writing any aggregation SQL?
"How much?" or "How many?" — the answer tells you SUM (how much) or COUNT (how many). Everything else follows.
2. What data types can SUM be applied to?
Only Integer and Real. Never Text, Character, or Boolean.
3. What's the difference between COUNT(*) and COUNT(FieldName)?
COUNT(*) counts every matching record (looks at rows). COUNT(FieldName) counts records where that field has a non-null value. On the tables in this lab they give the same answer — COUNT(*) is the simplest choice.
4. Can SUM or COUNT take more than one field?
No. Both take exactly one argument. SUM(Price, Weight) is invalid. Two totals = two separate SELECT statements.
5. What does an aggregation query return?
A single number. Never a list of records, never a sorted table. That's why ORDER BY makes no sense with SUM or COUNT.
6. In an aggregation query, when does WHERE run?
Before the aggregation. WHERE filters the records first; then SUM or COUNT reduces the filtered records to one number.
7. If a Cambridge question asks "Explain the purpose of SELECT SUM(Months) FROM Contract;", what three parts should your answer include?
What it does (totals a field), which field (Months), and which records (every contract). Covering all three is the clearest answer.
8. Write an SQL script that returns the total NumberInStock for every drink sold in a Can from the SoftDrinks table.
SELECT SUM(NumberInStock)
FROM SoftDrinks
WHERE Container = "Can";
Key checks: SUM (not COUNT — it's asking for total stock), NumberInStock (the numeric field), "Can" quoted (Text), ; at the end.
9. Using the SoftDrinks table above, what value does SELECT COUNT(*) FROM SoftDrinks WHERE Supplier = "Fenland Drinks"; return?
3 — Lemon Splash, Apple Crush and Berry Blast. Write just the number.

✅ Topic 9 complete

That's the last skill in the Databases topic. From designing a table (Foundations) to querying it (Querying) to totals and counts (this lab), you now have every SQL keyword in the syllabus list.

Next up: Topic 10 Boolean Logic — logic gates, truth tables and logic expressions.

🎓 Learn — walking-around understanding

Book Notes is the reference. Learn is the walk-through. If SUM and COUNT are new, start here and go slow. If Book Notes already clicked, skim.

Concept 1 · Aggregation = many records → one number

Every query you wrote in the Querying Lab returned rows. Some rows, all rows, sorted rows — but always rows. Aggregation is different. It reads all the matching records and returns one single number.

📊

Querying Lab: SELECT City FROM MajorCity WHERE Continent = "Asia";
→ returns 3 rows (Abu Dhabi, Beijing, Jakarta).

🎯

This lab: SELECT COUNT(*) FROM MajorCity WHERE Continent = "Asia";
→ returns 1 number (3).

The mental model

Aggregation is a funnel. WHERE selects records; SUM or COUNT squeezes them down to one value. Same table, same WHERE — different aggregation, different result.

Concept 2 · "How much?" or "How many?" — the one question

Before you write any SQL, look at the plain-English question and ask yourself which of these two phrases fits:

➕

"How much" = SUM
Total. Altogether. Combined. Overall stock. Total minutes. → SUM(FieldName)

🔢

"How many" = COUNT
Number of. Count of. How many records. → COUNT(*) or COUNT(FieldName)

Sanity test. "How many cans of Cola are in stock?" — that's ambiguous! It could mean:

  • "How many kinds of Can drink do we sell?" → COUNT records where Container = "Can"
  • "How much Can-drink stock do we hold?" → SUM the NumberInStock field for Can drinks

Exam questions are usually clearer, but always re-read carefully — the right function matters more than perfect syntax.

Concept 3 · SUM works only on numbers

You can add up prices. You can add up populations. You can add up minutes. You cannot add up names, yes/no flags, or genres.

✅

Sum-able: Integer fields, Real fields. Anything with numeric values.
SUM(Price), SUM(Population), SUM(NumberInStock).

❌

Not sum-able: Text, Character, Boolean, Date/Time.
SUM(CityName), SUM(Available), SUM(Genre) — all invalid.

The check

Before writing SUM, look at the data type of the field. If it's not Integer or Real, you're using the wrong function. If the question is "how much" but the field isn't numeric — either you need to count records instead, or you need to pick a different field.

Concept 4 · WHERE still runs first

When you combine aggregation with a WHERE clause, the order of operations is important:

  1. FROM — pick the table.
  2. WHERE — narrow to the matching records.
  3. SUM/COUNT — squeeze those records to one number.

Every WHERE rule from the Querying Lab still applies. Text values quoted (= "Can"). Booleans unquoted (= TRUE). Numbers unquoted (> 5.00). Relational operators =, <>, <, <=, >, >=. Logical operators AND, OR.

The exam trap

The 2024 s24 examiner report noted answers that used SUM(NumberInStock) correctly but missed the quotes around "Can" in the WHERE. The Querying Lab rules still matter.

You've got Learn — head to Activities

Time to practise the "how much? how many?" call at SUM or COUNT? — the signature interactive for this lab. Sample tables, live results and instant feedback — then try the Number Predictor.

🎮 SUM or COUNT?

Read the plain-English business question. Pick SUM or COUNT. Complete the SQL. The bench runs your query against a live sample table and checks it against the rules that matter in the exam.

📋 The table (sample data)
🎯 Business question
1️⃣ Step 1 — pick your function

You haven't picked yet.

2️⃣ Step 2 — complete the SQL
👀 Result

Pick your function and run your query to see the aggregate result.

🎯 Marker

Feedback lands here after you run — checks on function choice, field, quoting, WHERE structure, and the value returned.

🔢 Number Predictor

Read the query, use the table, and type the single number it returns. Filter first, then count or add up.

What SUM or COUNT? is testing

  • Function choice — did you pick the one the question actually needs?
  • Field and table — for SUM, the right numeric field; for COUNT, COUNT(*).
  • WHERE clause — field, operator and value, and only when the question filters.
  • Quotes — text in quotes; numbers and TRUE/FALSE without.
  • Result — does your query return the same number as the model answer?

⚡ 60-second sprint

Rapid-fire questions on SUM, COUNT and WHERE. Sixty seconds on the clock. Beat your best score.

✎ Practice questions

Multiple-choice checks. Start on Easy if you're new to the topic; move up as you go. Every attempt is logged in Progress. Adaptive: weak skills come up more often.

All
Easy
Medium
Exam-style
🧪 Exam Mode
🧪 Exam Mode active. Options are hidden — recall the answer in your head, then reveal and self-mark.

📋 Exam-style questions

Paper 2 style: write a query, state the value a query returns, or explain a query. Press Mark my answer — SQL answers are run and checked point by point. Skills feed into Mastery.

🔄 Review

Mark-losing traps on SUM and COUNT, memory triggers for the key habits, and your own mistakes.

⚠️ 10 aggregation traps to know

Read them before every mock exam.

Trap 1 · COUNT when the question wants SUM

"Total", "altogether" or "how much" → SUM. COUNT(NumberInStock) counts drinks; it doesn't total the stock.

Cited: 2024 s24 P2 Q9(b) examiner report.

Trap 2 · SUM when the question wants COUNT

"How many" or "number of" → COUNT. SUM(Supplier) tries to add up text.

Common exam mistake

Trap 3 · SUM on a non-numeric field

SUM adds up Integer and Real fields only. Check the field's data type first.

Common exam mistake

Trap 4 · Missing quotes on text values

WHERE Container = "Can" — text values go in quotes.

Cited: 2024 s24 P2 Q9(b) examiner report.

Trap 5 · Quoted Booleans or numbers

WHERE Available = TRUE, WHERE Price > 5 — no quotes.

Common exam mistake

Trap 6 · Missing WHERE when the question filters

"Total stock of drinks in cans" — the filter needs WHERE Container = "Can".

Cited: 2024 s24 P2 Q9(b).

Trap 7 · Two fields in one SUM or COUNT

One field in the brackets. Two totals = two queries.

Common exam mistake

Trap 8 · ORDER BY with SUM or COUNT

One number has nothing to sort — leave ORDER BY out.

Common exam mistake

Trap 9 · Vague "explain the purpose" answers

Say what it does, which field and which records.

Common exam mistake

Trap 10 · Expecting a list from SUM or COUNT

The result is one number. A list of records needs a separate SELECT.

Common exam mistake

🧠 Memory triggers

Short mnemonics for the exam-critical aggregation habits.

🗝️ "How much? or How many?"

How much → SUM. Total. Altogether. Combined.
How many → COUNT. Number of. Count of.
Ask this before you write a single character of SQL.

🗝️ "SUM is Numeric Only" (SNO)

SUM works on Numbers Only. Integer and Real. Never Text, Character, Boolean, or Date/Time. Before writing SUM, glance at the data type of the field. If it's not numeric, either you need COUNT or you need a different field.

🗝️ "COUNT-Star"

COUNT(*) counts the matching records. On the tables you'll meet, COUNT(FieldName) gives the same answer — COUNT(*) is the simplest choice.

🗝️ "One In, One Out"

One argument in the parentheses. One number out as the result. Never two fields in SUM(). Never a table of records. Aggregation is a funnel — many records in, one number out.

🗝️ "Words in quotes" (still true!)

The WHERE quoting rule from the Querying Lab is unchanged. Text values quoted (= "Can"). Booleans unquoted (= TRUE). Numbers unquoted (> 5). The 2024 s24 examiner report noted answers that used SUM correctly but missed the quotes around "Can".

🗝️ "What · Which · Which"

The three-part "explain the purpose" answer:
What it does (totals a field / counts records) → Which field → Which records. Cover all three for a clear answer.

Your logged mistakes

No mistakes logged yet. Come back after Practice and Exam.

Recent Vault entries

Nothing in the Vault yet. Save key facts from Book Notes or from question feedback.

🏆 Mastery

Every skill you attempt (in SUM or COUNT?, Practice, or Exam) is scored. Get 3+ attempts with 70%+ accuracy on a skill and it goes green — mastered.

Tap a skill badge to override its status (e.g. mark it Mastered by hand once you're confident, or set it back to In Progress to force more practice).

No attempts yet.

📝 Revision checklist

Tick each item as you cover it. Save your progress by leaving this browser open.

Achievements

Complete Practice and Exam to unlock achievements.

✅ Topic 9 complete

That's the end of Topic 9: Databases. Next, Topic 10 covers Boolean logic: logic gates, truth tables and logic expressions.

Mistake log

Mistakes

Your recent mistakes

Progress

Stats & progress

Practice

0Attempts
–Accuracy

Exam

0Attempts
–Average key points
Bookmarks

Bookmarked questions

No bookmarks yet. Bookmark a question in the Exam tab to save it here.

My notes

My notes

Add note

Title
Note

Your notes

📝 Notes on this page

Spot a bug, a confusing explanation, or something to improve? Jot it down here. These notes are saved on this device only — they are not sent to anyone.

Category
Details
Knowledge Vault 2.0

Knowledge Vault

Add entry

Category
Confidence
Title
Info

Entries

Saved ✓