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.
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.
SUM and COUNT to query a single table, with or without WHERE (and AND / OR).SUM or COUNT query — the single value it returns.SUM for "how much?" (a total) and COUNT for "how many?" (a number of records).SELECT SUM(FieldName) FROM Table; — knowing SUM only adds up Integer or Real fields.SELECT COUNT(*) FROM Table; to count records.WHERE clause so only matching records are totalled or counted.SUM or COUNT statement does.| Term | Meaning |
|---|---|
| Aggregate function | A 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 field | A 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 total | WHERE 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.
Before you write a single character of SQL, read the question and ask yourself:
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.
Syntax:
SELECT SUM(FieldName) FROM Table WHERE Condition; (optional)
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;
SELECT COUNT(*) FROM Table WHERE Condition; (optional)
COUNT(*) counts records, so it doesn't matter what is in the fields.COUNT(FieldName), e.g. COUNT(Title). On the tables in this lab it gives the same answer.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.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).
SUM(Price, Weight) is invalid — one field at a time.ORDER BY. There's nothing to sort — you get one number.A question may give you an SQL statement and ask you to explain its purpose. A clear answer covers three things:
You may also be asked for the value a query returns. Work it out in two steps:
WHERE clause to pick out the matching records.COUNT → count them. SUM → add up the field for those records only.Write just the number — no working, no units, no punctuation.
The examples use one SoftDrinks sample table, in the style of a recent Paper 2 question.
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).
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.
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.
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.
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.
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.
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?".
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.
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(*).
❌ 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.
❌ 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.
❌ 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❌ 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.
❌ 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.❌ 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❌ Writing SELECT SUM(Price, SizeM3) FROM StorageUnit;
SUM and COUNT take one field. Two totals need two separate queries.
Common exam mistake❌ 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❌ Writing SELECT SUM(Months) FROM Contract ORDER BY Months;
One number has nothing to sort, so ORDER BY isn't needed. Leave it out.
❌ 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 mistakeHabits that keep marks safe.
COUNT(*) is the simplest form.SUM(Price, Weight) is not valid.;.Click each question to reveal the answer. 0 / 9 revealed
SUM be applied to?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.SUM or COUNT take more than one field?SUM(Price, Weight) is invalid. Two totals = two separate SELECT statements.ORDER BY makes no sense with SUM or COUNT.SELECT SUM(Months) FROM Contract;", what three parts should your answer include?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.SELECT COUNT(*) FROM SoftDrinks WHERE Supplier = "Fenland Drinks"; return?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.
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.
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).
Aggregation is a funnel. WHERE selects records; SUM or COUNT squeezes them down to one value. Same table, same WHERE — different aggregation, different result.
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:
Exam questions are usually clearer, but always re-read carefully — the right function matters more than perfect syntax.
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.
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.
When you combine aggregation with a WHERE clause, the order of operations is important:
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 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.
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.
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.
You haven't picked yet.
Pick your function and run your query to see the aggregate result.
Feedback lands here after you run — checks on function choice, field, quoting, WHERE structure, and the value returned.
Read the query, use the table, and type the single number it returns. Filter first, then count or add up.
COUNT(*).Rapid-fire questions on SUM, COUNT and WHERE. Sixty seconds on the clock. Beat your best score.
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.
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.
Mark-losing traps on SUM and COUNT, memory triggers for the key habits, and your own mistakes.
Read them before every mock exam.
"Total", "altogether" or "how much" → SUM. COUNT(NumberInStock) counts drinks; it doesn't total the stock.
"How many" or "number of" → COUNT. SUM(Supplier) tries to add up text.
SUM adds up Integer and Real fields only. Check the field's data type first.
Common exam mistakeWHERE Container = "Can" — text values go in quotes.
WHERE Available = TRUE, WHERE Price > 5 — no quotes.
"Total stock of drinks in cans" — the filter needs WHERE Container = "Can".
One field in the brackets. Two totals = two queries.
Common exam mistakeOne number has nothing to sort — leave ORDER BY out.
Common exam mistakeSay what it does, which field and which records.
Common exam mistakeThe result is one number. A list of records needs a separate SELECT.
Common exam mistakeShort mnemonics for the exam-critical aggregation habits.
How much → SUM. Total. Altogether. Combined.
How many → COUNT. Number of. Count of.
Ask this before you write a single character of SQL.
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(*) counts the matching records. On the tables you'll meet, COUNT(FieldName) gives the same answer — COUNT(*) is the simplest choice.
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.
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".
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.
No mistakes logged yet. Come back after Practice and Exam.
Nothing in the Vault yet. Save key facts from Book Notes or from question feedback.
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.
Tick each item as you cover it. Save your progress by leaving this browser open.
Complete Practice and Exam to unlock achievements.
That's the end of Topic 9: Databases. Next, Topic 10 covers Boolean logic: logic gates, truth tables and logic expressions.
No bookmarks yet. Bookmark a question in the Exam tab to save it here.
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.