
How many biscuits were ordered altogether?
Jacques needs to know how many biscuits to bake for today's orders. The orders are stored in the BiscuitOrders table, and the number of biscuits in each order is held in the Quantity field.
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
He could add up the Quantity column by hand. SQL does the same job with one short query:
SELECT SUM(Quantity) FROM BiscuitOrders;
6 + 4 + 8 + 2 = 20 biscuits in total.
Cambridge syllabus focus
Topic 9: understand and complete SQL scripts that query a single table using SELECT, FROM, WHERE, ORDER BY, SUM and COUNT. New to SELECT, FROM and WHERE? Start with SQL SELECT and FROM Explained and SQL WHERE Explained.
Reading the query line by line
SELECT SUM(Quantity)means βadd together the values in the Quantity fieldβ.FROM BiscuitOrdersmeans βuse the BiscuitOrders tableβ.- The output is one number. You do not get a list of records or a column of values.
Worked example: how many Hobnobs were ordered?
Now Jacques only wants the Hobnob total. Add a WHERE condition so that only the Hobnob records are included:
SELECT SUM(Quantity) FROM BiscuitOrders WHERE BiscuitType = 'Hobnob';
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
- WHERE selects the matching records. The database checks BiscuitType in each record. Records 101, 103 and 104 contain Hobnob. Record 102 contains Viennese Swirl, so it is left out.
- SUM adds the values in the specified numeric field. Only the Quantity values from the three matching records are added: 6 + 8 + 2.
- One total is returned.
16 Hobnobs were ordered.
'Hobnob'). Numbers do not (Quantity > 5). Write the text value exactly as it is stored in the table.Check your understanding 1
What would this query output?
SELECT SUM(Quantity) FROM BiscuitOrders WHERE BiscuitType = 'Viennese Swirl';
Reveal answer
4. Only record 102 matches, so its single Quantity value of 4 is the total.
How WHERE and SUM share the work
WHERE works across the rows
It decides which records are included: the three Hobnob orders.
SUM works down one column
It adds the values in one numeric field: 6 + 8 + 2 = 16.
Common Cambridge exam mistake: adding the wrong field
The question asks how many Hobnobs were ordered, but this query adds the order numbers:
SELECT SUM(OrderID) FROM BiscuitOrders WHERE BiscuitType = 'Hobnob';
101 + 103 + 104 = 308. This number means nothing.
OrderID is stored as an integer, but its values are labels that identify orders. They are not amounts. The field inside SUM must hold the amounts you want to total, so the correct field is Quantity, which gives 16.
Check your understanding 2
OrderID contains whole numbers. Why is it still wrong to use it inside SUM?
Reveal answer
OrderID values identify orders. They do not measure anything. Adding them gives a number (308) that does not answer any useful question.
Original FutureLogic exam-style practice
These are original teaching questions, not official Cambridge examination questions. The marks shown are a guide only.
Question 1 (3 marks)
Write an SQL query to output the total number of Viennese Swirls ordered from the BiscuitOrders table.
Reveal model answer
SELECT SUM(Quantity) FROM BiscuitOrders WHERE BiscuitType = 'Viennese Swirl';
1 mark for SELECT SUM(Quantity), 1 mark for FROM BiscuitOrders and 1 mark for WHERE BiscuitType = 'Viennese Swirl'. (The query outputs 4.)
Question 2 (2 marks)
State the output of this query and show how you found it.
SELECT SUM(Quantity) FROM BiscuitOrders WHERE Quantity > 5;
Reveal model answer
Records 101 (6) and 103 (8) are the only ones with a Quantity greater than 5 (1). Output: 6 + 8 = 14 (1).
Quick quiz
- How many values does a SUM query return?
- Which field should be added to find the number of biscuits ordered?
- What does
SELECT SUM(Quantity) FROM BiscuitOrders;output? - In a SUM query, what does WHERE do?
Quiz answers
Reveal all answers
- One total.
- Quantity.
- 20.
- It chooses which records are included before their values are added.
Final summary
SUM
Adds the values in one numeric field.
WHERE
Chooses which records are included.
Output
One total: 20 overall, 16 for Hobnobs.
Right field
Add Quantity, never OrderID.