Structured Query Language · Cambridge syllabus 9.4 · Lesson 2 of 4
Filtering Data
WHERE selects records that satisfy a condition. Use comparisons and combine conditions with AND or OR as required. Text values are quoted; field names identify stored columns.
By the end, you can
- Use conditions to filter records.
- Use comparison operators.
- Combine conditions using AND and OR.
WHERE conditions
A WHERE clause restricts a query to the records that satisfy a condition, and it is where most of the work of a query happens. SELECT Title FROM Books WHERE Author = "Achebe" returns only the matching records.
The comparison operators are the familiar ones: = for equal, <> for not equal, and <, >, <= and >=. They work on numbers, on dates, and on text, which compares alphabetically — so WHERE Surname < "M" selects the first half of an alphabetical list.
Conditions combine with AND and OR, and the difference matters: AND narrows the result, since both must hold, while OR widens it. WHERE Price < 20 AND Category = "Science" returns cheap science books; changing AND to OR returns everything cheap plus everything scientific, which is a much longer and quite different list. Brackets make the intention explicit when both appear in one condition.
A range is written with two comparisons joined by AND: WHERE Year >= 2018 AND Year <= 2024 selects the books from 2018 to 2024, including both years. Either of two values is written with OR: WHERE Category = "Science" OR Category = "History". Each side of AND or OR must be a complete comparison, field name and all.
The SQL in the syllabus is limited to SELECT, FROM, WHERE, ORDER BY with ascending or descending order, SUM, COUNT, AND and OR, so every condition in an exam query is built from comparisons joined by AND and OR.
Worth remembering
- WHERE filters the records; AND narrows the result, OR widens it.
- A range is two comparisons joined by AND, each written in full.
- Text values go in quotation marks; numbers do not.
Worked example
SELECT Name FROM Students WHERE Score >= 50 returns names for qualifying rows.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
SELECT Name FROM Students WHERE Score < 40
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.