
How many orders were placed?
This time Jacques does not want to know how many biscuits were ordered. He wants to know how many orders came in. Each order is one record in the BiscuitOrders table, so the question is really “How many records are there?”
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
SELECT COUNT(OrderID) FROM BiscuitOrders;
Four records: orders 101, 102, 103 and 104.
Cambridge syllabus focus
Topic 9: understand and complete SQL scripts that query a single table using SELECT, FROM, WHERE, ORDER BY, SUM and COUNT. If you need a refresher, read SQL WHERE Explained first.
Why counting the primary key works
COUNT(field) counts the records that hold a value in that field. OrderID is the table's primary key, so every record must have an OrderID, and no two records share one. Counting OrderID therefore counts every matching record exactly once.
Worked example: how many Hobnob orders?
Add a WHERE condition so that only Hobnob orders are counted:
SELECT COUNT(OrderID) 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. Records 101, 103 and 104 have Hobnob as their BiscuitType. Record 102 does not.
- COUNT counts the records that remain: 1, 2, 3.
- One number is returned.
Three Hobnob orders were placed.
Check your understanding 1
What would this query output?
SELECT COUNT(OrderID) FROM BiscuitOrders WHERE BiscuitType = 'Viennese Swirl';
Reveal answer
1. Only record 102 matches. COUNT returns the number of records (1), not that record's Quantity (4).
COUNT(*): counting the rows directly Core
You can also write COUNT(*). The asterisk means “count the rows”, so SQL does not check any particular field. This is the clearest way to count records, and it is the form used in our Knowledge Library COUNT lesson.
SELECT COUNT(*) FROM BiscuitOrders WHERE BiscuitType = 'Hobnob';
This also outputs 3. For BiscuitOrders, COUNT(*) and COUNT(OrderID) always give the same answer, because every record has an OrderID.
COUNT(field) and empty values Extension
COUNT(field) only counts the records where that field contains a value. A missing value is stored as NULL, and COUNT(field) skips it. For example, suppose a smudged order slip was saved as order 105, a Hobnob order with its Quantity left empty (NULL). This record is not part of our BiscuitOrders table. It is only here to show the difference:
| Query | Output | Why |
|---|---|---|
COUNT(*) | 5 | Counts every row |
COUNT(OrderID) | 5 | Every row has an OrderID |
COUNT(Quantity) | 4 | Skips the NULL Quantity |
This is useful background. Cambridge 0478 questions normally work with tables that have no missing values.
Common Cambridge exam mistake: using SUM for “how many orders?”
SELECT SUM(Quantity) FROM BiscuitOrders WHERE BiscuitType = 'Hobnob';
That is the number of biscuits, not the number of orders.
The question asks how many records match, so it needs COUNT, which gives 3.
Same data, two different questions
Both queries below use the same three Hobnob records, but they ask different questions:
COUNT(OrderID)
3Hobnob orders: one for each matching record.
SUM(Quantity)
16Hobnobs ordered: 6 + 8 + 2.
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 number of orders in BiscuitOrders that have a Quantity greater than 3.
Reveal model answer
SELECT COUNT(OrderID) FROM BiscuitOrders WHERE Quantity > 3;
1 mark for SELECT COUNT(OrderID) (accept COUNT(*)), 1 mark for FROM BiscuitOrders and 1 mark for WHERE Quantity > 3. (Orders 101, 102 and 103 match, so the query outputs 3.)
Question 2 (2 marks)
With the condition WHERE BiscuitType = 'Hobnob', COUNT(OrderID) outputs 3 but SUM(Quantity) outputs 16. Explain why the two outputs are different.
Reveal model answer
COUNT returns the number of matching records/orders, which is 3 (1). SUM adds the values in the Quantity field of those records, 6 + 8 + 2, giving 16 (1).
Quick quiz
- What does COUNT return?
- Why is OrderID a good field to count?
- What does
SELECT COUNT(*) FROM BiscuitOrders;output? - Which function answers “How many biscuits were ordered?”
Quiz answers
Reveal all answers
- The number of records in the result.
- It is the primary key, so every record has a unique OrderID and is counted exactly once.
- 4.
- SUM, because the question asks for a total quantity, not a number of records.
Final summary
COUNT
Returns how many records match.
WHERE
Chooses which records are counted.
COUNT(*)
Counts rows directly. COUNT(field) skips NULLs.
COUNT vs SUM
3 Hobnob orders, 16 Hobnobs ordered.