FutureLogic Blog β†’ Databases β†’ IGCSE 0478

How Does SQL SUM Add Values?
Explained Simply

The SQL SUM function adds together all the values in a single numeric field and returns one total. When combined with a WHERE clause, SQL filters the table first so that only values from matching records are included in the final sum.

Use the Golden Biscuit Tin's BiscuitOrders table to see how SELECT SUM(Quantity) works with and without WHERE, and avoid common exam mistakes for Cambridge IGCSE Computer Science (0478 and 0984).

FutureLogic Education thumbnail: SQL SUM – Add Values, with Jacques the pigeon tipping three trays of biscuits into the Golden Biscuit Tin while a surprised chicken in glasses watches

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.

BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2

He could add up the Quantity column by hand. SQL does the same job with one short query:

SELECT SUM(Quantity)
FROM BiscuitOrders;
Output20

6 + 4 + 8 + 2 = 20 biscuits in total.

SUM adds the values in one numeric field and returns one total. It does not list the records.

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 BiscuitOrders means β€œ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';
Matches WHEREValue added by SUMDoes not match
BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
  1. 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.
  2. SUM adds the values in the specified numeric field. Only the Quantity values from the three matching records are added: 6 + 8 + 2.
  3. One total is returned.
Only the matching Hobnob quantities are addedFour order cards. Orders 101, 103 and 104 are Hobnob orders with quantities 6, 8 and 2 and are highlighted. Order 102, Viennese Swirl with quantity 4, is greyed out and not added. The total shown is 6 plus 8 plus 2 equals 16. WHERE BiscuitType = 'Hobnob' β†’ SUM(Quantity) Order 101HobnobQuantity 6 Order 102Viennese Swirl4 Β· not added Order 103HobnobQuantity 8 Order 104HobnobQuantity 2 6 + 8 + 2 = 16
Diagram 1: WHERE keeps the three Hobnob records. SUM then adds their Quantity values.
Output16

16 Hobnobs were ordered.

Exam tip: Text values in a WHERE condition go inside quotation marks ('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

βœ— Choosing OrderID instead of Quantity

The question asks how many Hobnobs were ordered, but this query adds the order numbers:

SELECT SUM(OrderID)
FROM BiscuitOrders
WHERE BiscuitType = 'Hobnob';
Output308

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.

Before you write SUM, ask: β€œWhich field holds the amounts I want to add?” Biscuits ordered, prices and scores make sense to add. ID numbers do not.

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

  1. How many values does a SUM query return?
  2. Which field should be added to find the number of biscuits ordered?
  3. What does SELECT SUM(Quantity) FROM BiscuitOrders; output?
  4. In a SUM query, what does WHERE do?

Quiz answers

Reveal all answers
  1. One total.
  2. Quantity.
  3. 20.
  4. 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.

WHERE chooses the records. SUM adds one field's values. Result: 16 Hobnobs ordered.

Ready to count the orders instead?

SUM told us how many biscuits were ordered. COUNT tells us how many orders there were, using the same BiscuitOrders table.