Cambridge IGCSE · Topic 9 (4)
IGCSE Computer Science 0478 / Topic 9 / IGCSE

Many rows, one answer.

How many games can be played online? What is the total price of the adventure games? SUM and COUNT turn a list of matching records into a single number, and AND and OR let one WHERE clause test two conditions at once.

The key idea

SUM(field) adds up the values in a numeric field for every record that matches the WHERE condition. COUNT(field) gives the number of records that match. Both return a single value, not a list. In a WHERE clause, AND keeps a record only if both conditions are true; OR keeps a record if at least one condition is true. A record that meets both OR conditions is still output only once.

WHERE Genre = 'Racing' OR Price >= 35 G01 Racing 25.00 G02 Adventure 40.00 G03 Puzzle 10.00 G04 Racing 35.00 G05 Adventure 30.00 G06 Puzzle 15.00 COUNT(GameID) 3 SUM(Price) 25 + 40 + 35 = 100.00 matching rows become one value
Fig. 1: three records match the condition; COUNT gives 3 and SUM adds their prices
Section 01

SUM and COUNT.

In the lab, run a SUM or COUNT query on the Books table and compare it with the same query without them: the list of rows collapses into one number. In this lesson every query again uses the GAMES table.

GameIDTitleGenrePriceYearOnline
G01Star RacerRacing25.002021TRUE
G02Castle QuestAdventure40.002019FALSE
G03Puzzle PopPuzzle10.002023TRUE
G04Speed KingsRacing35.002022TRUE
G05Dragon IsleAdventure30.002020FALSE
G06Block TownPuzzle15.002018FALSE
Worked example SUM
Give the output of this query.
SELECT SUM(Price)
FROM Games
WHERE Genre = 'Adventure';
  1. Find the Adventure records: G02 (40.00) and G05 (30.00).
  2. Add their prices: 40.00 + 30.00.
Answer: 70.00
Worked example COUNT
Give the output of this query.
SELECT COUNT(GameID)
FROM Games
WHERE Online = TRUE;
  1. Check Online for each record: G01 TRUE, G02 FALSE, G03 TRUE, G04 TRUE, G05 FALSE, G06 FALSE.
  2. Three records match; COUNT gives how many, not which ones.
Answer: 3
Your turn
Give the output of: SELECT SUM(Price) FROM Games;
Examiner trap

SUM adds the values; COUNT counts the records. COUNT(Price) on the whole GAMES table gives 6, not 155.00.

Section 02

Two conditions: AND and OR.

Write the full condition on each side of AND or OR: Genre = 'Racing' AND Price > 30, not Genre = 'Racing' AND > 30. Then test each record against both parts.

Condition 1Condition 2ANDOR
falsefalsenot outputnot output
falsetruenot outputoutput
truefalsenot outputoutput
truetrueoutputoutput
Worked example AND
Give the output.
SELECT Title
FROM Games
WHERE Genre = 'Racing' AND Price > 30;
  1. The Racing records are G01 (25.00) and G04 (35.00).
  2. Only G04 also has Price > 30. G01 fails the second condition.
Answer: Speed Kings
Worked example OR
Give the output.
SELECT Title
FROM Games
WHERE Genre = 'Puzzle' OR Year < 2020;
  1. Puzzle: G03 Puzzle Pop and G06 Block Town.
  2. Year < 2020: G02 Castle Quest (2019) and G06 Block Town (2018).
  3. Block Town meets both but appears once. Output in table order.
Answer: Castle Quest; Puzzle Pop; Block Town
Your turn
Give the output of: SELECT COUNT(GameID) FROM Games WHERE Price > 20 AND Year > 2019;
Examiner trap

AND gives the same or fewer records than either condition alone; OR gives the same or more. If your AND answer has more rows than one of its conditions, recheck it.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What does COUNT(GameID) return in an SQL query?

Quick check+10 XP

In the lab, what is the output of SELECT COUNT(BookID) FROM Books WHERE Genre = 'Sci-fi';?

Quick check+10 XP

A WHERE clause joins two conditions with AND. When is a record output?

Quick check+10 XP

What is the output of SELECT SUM(Price) FROM Games WHERE Genre = 'Puzzle'; on the GAMES table?

Section 03

Putting it together.

Exam questions combine everything: a function, two conditions and sometimes a sort. Use the lab's practice questions to check your method, then work through the example below one record at a time.

SELECT SUM(Price)
FROM Games
WHERE Online = TRUE AND Price < 30;
Worked example SUM with AND
Give the output of the query above.
  1. Online = TRUE: G01 (25.00), G03 (10.00), G04 (35.00).
  2. Of these, Price < 30: G01 and G03. G04 fails.
  3. SUM adds their prices: 25.00 + 10.00.
Answer: 35.00
Your turn
Complete the query to count the Adventure games that cost more than 35.00, and give its output.
SELECT ........(GameID)
FROM Games
WHERE Genre = 'Adventure' ........ Price > 35;
Examiner trap

When asked for the output of a SUM or COUNT query, give one value. Listing the matching records instead earns no mark.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is the output of SELECT COUNT(GameID) FROM Games WHERE Genre = 'Adventure' OR Online = TRUE;?

Exam style+20 XP

What is the output of SELECT Title FROM Games WHERE Price < 30 AND Online = FALSE;?

Exam style+20 XP

Which query gives the total price of all the Racing games?

Exam style+20 XP

What is the output of SELECT SUM(Price) FROM Games WHERE Year >= 2021 AND Genre <> 'Racing';?

Skill unlocked

SUM, COUNT, AND and OR, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
SQL SUM and COUNT, combining conditions with AND and OR, and giving the output of a query.