FutureLogic Blog → Databases → IGCSE 0478

How Does SQL COUNT Count Records?
Explained Simply

The SQL COUNT function returns the total number of records that match a query. When paired with a WHERE clause, COUNT tallies only the rows that meet your condition without adding together the values stored inside those fields.

Use the Golden Biscuit Tin's BiscuitOrders table to count all orders, filter specific records with WHERE, and see exactly how COUNT differs from SUM for Cambridge IGCSE Computer Science (0478 and 0984).

FutureLogic Education thumbnail: SQL COUNT – Count Matching Records, with Jacques the pigeon, a chicken holding a magnifying glass and a pirate parrot checking four ticked biscuit order cards

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?”

BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
SELECT COUNT(OrderID)
FROM BiscuitOrders;
Output4

Four records: orders 101, 102, 103 and 104.

COUNT answers “how many records?” It never adds the values inside them.

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.

Exam tip: To count the records, count a field that every record is guaranteed to fill. The primary key is always the safest choice.

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';
Matches WHERE: countedDoes not match
BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
  1. WHERE selects the matching records. Records 101, 103 and 104 have Hobnob as their BiscuitType. Record 102 does not.
  2. COUNT counts the records that remain: 1, 2, 3.
  3. One number is returned.
Each matching record counts as oneFour order cards. The Hobnob orders 101, 103 and 104 are ticked and numbered 1, 2 and 3. Viennese Swirl order 102 is crossed out. The result box says COUNT equals 3, and a note explains that the quantities are not added. WHERE BiscuitType = 'Hobnob' → COUNT the records 1Order 101Hobnob✓ counted Order 102Viennese Swirl✗ not counted 2Order 103Hobnob✓ counted 3Order 104Hobnob✓ counted COUNT = 3 records The quantities 6, 8 and 2 are not added. Each matching record counts as 1.
Diagram 1: COUNT counts the matching records. The values stored inside them do not change the answer.
Output3

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:

If the table also held order 105 with no Quantity
QueryOutputWhy
COUNT(*)5Counts every row
COUNT(OrderID)5Every row has an OrderID
COUNT(Quantity)4Skips 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?”

✗ Answering “How many Hobnob orders were placed?” with SUM
SELECT SUM(Quantity)
FROM BiscuitOrders
WHERE BiscuitType = 'Hobnob';
Output16

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.

Read the question carefully: “How many orders, records or customers…?” needs COUNT. “What is the total quantity, cost or score…?” needs SUM.

Same data, two different questions

Both queries below use the same three Hobnob records, but they ask different questions:

COUNT(OrderID)

3

Hobnob orders: one for each matching record.

SUM(Quantity)

16

Hobnobs ordered: 6 + 8 + 2.

3 Hobnob orders. 16 Hobnobs ordered. COUNT counts the records. SUM adds their values.

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

  1. What does COUNT return?
  2. Why is OrderID a good field to count?
  3. What does SELECT COUNT(*) FROM BiscuitOrders; output?
  4. Which function answers “How many biscuits were ordered?”

Quiz answers

Reveal all answers
  1. The number of records in the result.
  2. It is the primary key, so every record has a unique OrderID and is counted exactly once.
  3. 4.
  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.

COUNT counts records. SUM adds values. Read the question to choose the right one.

Ready to master Topic 9?

You can now describe a table's structure, filter it, total it and count it. Work through the Topic 9 lessons to practise every SQL keyword.