← IGCSE Computer Science Paper 2

Mixing AND and OR with brackets in a WHERE clause

Brackets make SQL work out the OR first, then the AND. A record must meet C and also A or B. Moving the brackets, as in A OR (B AND C), can return different records.

Understanding Mixing AND and OR with brackets in a WHERE clause in IGCSE Computer Science Paper 2

What it does: The OR on yearLevel is worked out first. Records then still need points above 100, so the AND condition applies to both year levels. With AND, both sides must be true. (A OR B) can be true, but if C is false the record is not returned. Using the same field twice with OR needs brackets before AND price < 20. The correct version returns only USB Drive and Water Bottle. The wrong one also returns every Tech product, such as Headphones. The brackets make the AND work first. A Red house student qualifies on their own, but a Blue house student must also be in Year 7.

Common mistakes: The opening bracket is missing, so the closing bracket on line 3 has no partner. It should be WHERE (house = 'Red'. The brackets make the OR work first, then the AND. The semicolon ends the statement too early, so the AND (...) part is never read. The semicolon belongs only at the very end of the whole WHERE clause.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
Which WHERE clause returns only Year 7 students who are in Red or Blue house?
WHERE (house = 'Red' OR house = 'Blue') AND yearLevel = 7WHERE house = 'Red' OR house = 'Blue' AND yearLevel = 7
Matches
WHERE yearLevel = 7 OR yearLevel = 8 AND house = 'Red'
↓
The brackets are missing. AND is worked out before OR, so all Year 7 students are returned whatever their house. The query should be WHERE (yearLevel = 7 OR yearLevel = 8) AND house = 'Red'.
Applying
Meanings
Term
WHERE (A OR B) AND C
Meaning
Brackets make SQL work out the OR first, then the AND. A record must meet C and also A or B. Moving the brackets, as in A OR (B AND C), can return different records.
Practice
SELECT firstName, house, yearLevel
Testing
Gaps
yearLevel = 7;
Check Answer
Spots
1SELECT firstName FROM students
2WHERE house = 'Red'

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Octopus' badge — Multi-Tasker, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which WHERE clause returns only Year 7 students who are in Red or Blue house?
A. WHERE (house = 'Red' OR house = 'Blue') AND yearLevel = 7 ✓
B. WHERE house = 'Red' OR house = 'Blue' AND yearLevel = 7
Brackets make the OR work first, so yearLevel = 7 applies to both houses. Without brackets, AND is worked out first, so every Red student is returned whatever their year.
2. In WHERE (A OR B) AND C, which part is worked out first?
A. The AND with C
B. The OR inside the brackets ✓
Brackets are worked out first. A OR B is checked, then the result must also meet C.
3. A record meets condition A but not C. Is it returned by WHERE (A OR B) AND C?
A. No, because C must also be true ✓
B. Yes, because A is true
With AND, both sides must be true. (A OR B) can be true, but if C is false the record is not returned.
4. A record meets condition A but not B or C. Is it returned by WHERE A OR (B AND C)?
A. Yes, because A alone is enough ✓
B. No, because B AND C is false
With OR, only one side needs to be true. A is true, so the record is returned even though B AND C is false.
5. The same conditions A, B and C are used, but the brackets are moved. Will the same records always be returned?
A. Yes, the order of the brackets makes no difference
B. No, moving the brackets can change the records returned ✓
(A OR B) AND C needs C to be true. A OR (B AND C) lets a record in with A alone. Different brackets can mean different output.

Match the Pairs

WHERE (house = 'Red' OR house = 'Blue') AND yearLevel = 7 → Returns only Year 7 students who are in Red house or Blue house.
WHERE house = 'Red' OR (house = 'Blue' AND yearLevel = 7) → Returns every Red house student, plus only the Year 7 students from Blue house.
Brackets round an OR on the same field mean a record must match one of those two values and also meet the extra AND condition. → WHERE (yearLevel = 7 OR yearLevel = 8) AND points > 100
Brackets round an AND mean those two conditions are checked together, so a record can also qualify by meeting the single condition on the other side of the OR. → WHERE points > 100 OR (house = 'Red' AND yearLevel = 7)
WHERE yearLevel = 7 OR yearLevel = 8 AND house = 'Red' → The brackets are missing. AND is worked out before OR, so all Year 7 students are returned whatever their house. The query should be WHERE (yearLevel = 7 OR yearLevel = 8) AND house = 'Red'.
WHERE (house = 'Red' AND house = 'Blue') AND yearLevel = 7 → AND is used between two values of the same field. A record cannot be in two houses at once, so no rows are returned. It should be (house = 'Red' OR house = 'Blue').

Key Term

WHERE (A OR B) AND C
Brackets make SQL work out the OR first, then the AND. A record must meet C and also A or B. Moving the brackets, as in A OR (B AND C), can return different records.

Fill in the Blanks

1. SELECT firstName FROM students WHERE (house = 'Red' OR house = 'Blue') AND yearLevel = 7;
Hint: Type the logical operator that makes the year level condition compulsory as well as the bracketed house check.
2. SELECT firstName FROM students WHERE (house = 'Red' OR house = 'Blue') AND yearLevel = 7;
Hint: Type the logical operator that lets a student match either of the two houses inside the brackets.
3. SELECT firstName FROM students WHERE house = 'Red' OR (house = 'Blue' AND yearLevel = 8);
Hint: Type the logical operator that makes both conditions inside the brackets true together.
4. SELECT name FROM products WHERE (category = 'Tech' OR category = 'Sport') AND stock > 0;
Hint: Type the field name that must be greater than zero so only items still available are returned.
5. SELECT firstName FROM students WHERE (yearLevel = 7 AND house = 'Red') OR points > 100;
Hint: Type the logical operator that lets a record qualify through the bracketed group or through the points condition.

Spot the Error

Question 1
A SELECT firstName FROM students
B WHERE house = 'Red'
C OR house = 'Blue')
D AND points > 100;
Line B — The opening bracket is missing, so the closing bracket on line 3 has no partner. It should be WHERE (house = 'Red'. The brackets make the OR work first, then the AND.
Question 2
A SELECT name FROM products
B WHERE category = 'Tech';
C AND (price < 20
D OR stock > 50);
Line B — The semicolon ends the statement too early, so the AND (...) part is never read. The semicolon belongs only at the very end of the whole WHERE clause.
Question 3
A SELECT name products
B WHERE (category = 'Tech'
C OR category = 'Sport')
D AND stock > 0;
Line A — The FROM keyword is missing, so SQL does not know which table to use. It should be SELECT name FROM products.
Question 4
A SELECT firstName FROM students
B WHERE [yearLevel = 7
C OR yearLevel = 8)
D AND house = 'Red';
Line B — SQL brackets must be round ( ), not square [ ]. The line should be WHERE (yearLevel = 7 so the OR on the same field is worked out first.
Question 5
A SELECT name, price,
B FROM products
C WHERE category = 'Tech'
D OR (category = 'Sport'
E AND price < 10);
Line A — There is an extra comma after the last field, so SQL expects another field before FROM. It should be SELECT name, price with no trailing comma.

Student with a class code? Join your class →