SUM and COUNT with a WHERE condition
WHERE filters the records first, then COUNT or SUM works only on the matching ones. This counts members from Leeds. If no record matches, COUNT gives 0.
Understanding SUM and COUNT with a WHERE condition in IGCSE Computer Science Paper 2
What it does: AND inside WHERE means a record must meet both conditions before COUNT includes it. No record has Town = 'Mars', so no records are counted and the result is 0. No records match the WHERE condition, so COUNT has nothing to count and gives 0. It does not give an error or the total. WHERE is applied before SUM, so the function only adds the Fee values of the matching records. The condition belongs in the WHERE clause, after FROM. The SUM brackets only hold the field to add up. A member lives in only one town, so AND would match no one and give 0. OR counts members in either town.
Common mistakes: SQL uses a single = to compare values, not ==. The line should be AND Paid = TRUE; so only paid members from Leeds are added up. Text values in a WHERE condition must be in quotes. It should be WHERE Town = 'Hull'; so COUNT only counts members from Hull.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 525–538) and word search (Modules 500–538).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
SELECT COUNT(*) FROM Member WHERE Town = 'Leeds';Fill in the Blanks
Spot the Error
Student with a class code? Join your class →