FutureLogic Blog → Databases → IGCSE 0478

How Does SQL WHERE Filter Records?
Explained Simply

The SQL WHERE clause filters database records by testing every row against a specific condition. Only records that match the condition are returned in the query output, allowing you to control which rows appear while SELECT controls which columns are shown.

Filter the Golden Biscuit Tin's BiscuitOrders table by text and numbers, and learn when to use quotation marks and comparison operators for Cambridge IGCSE Computer Science (0478 and 0984).

FutureLogic Education thumbnail: SQL WHERE – Filter Your Records, with Jacques the pigeon inspecting biscuit cards with a magnifying glass as a conveyor sorts them into two trays

Which orders were for Hobnobs?

A query using only SELECT and FROM returns every record in the table. (If that's new to you, start with SQL SELECT and FROM Explained.) Jacques only wants the Hobnob orders, so he adds a WHERE clause to the query.

BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
SELECT controls the columns. WHERE controls the rows.

Cambridge syllabus focus

Topic 9.4: understand and complete SQL scripts that use WHERE to return only the records that meet a condition.

Worked example 1: filtering by text

SELECT OrderID, Quantity
FROM BiscuitOrders
WHERE BiscuitType = 'Hobnob';
  • SELECT OrderID, Quantity chooses the fields to display.
  • FROM BiscuitOrders names the table.
  • WHERE BiscuitType = 'Hobnob' keeps only the records whose BiscuitType is Hobnob.
Matches WHEREField tested by WHEREDoes not match
BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
Output: orders 101, 103 and 104
OrderIDQuantity
1016
1038
1042
WHERE tests each record and keeps only the matchesFour records pass through a WHERE BiscuitType equals Hobnob test. Orders 101, 103 and 104 pass with a tick and go to the output, which shows only OrderID and Quantity. Order 102, Viennese Swirl, fails with a cross and is left out. EVERY RECORD IS TESTED 101 · Hobnob · 6 102 · Viennese Swirl · 4 103 · Hobnob · 8 104 · Hobnob · 2 WHERE BiscuitType = 'Hobnob' 101 ✓102 ✗103 ✓104 ✓ → SELECT OrderID, Quantity OrderIDQuantity 101610381042 BiscuitType is tested, but not displayed. 3 of the 4 records match, so 3 rows are output.
Diagram 1: WHERE tests each record against the condition. SELECT then decides which fields of the matching records are shown.

You can test a field without displaying it

Look closely at the output. BiscuitType is not in it, even though the WHERE condition uses BiscuitType. That's allowed. WHERE can test any field in the table, and SELECT decides separately which fields to show.

Exam tip: When you trace a query, apply WHERE to the whole record first. Then copy only the fields named after SELECT into your answer.

Check your understanding 1

What would SELECT OrderID FROM BiscuitOrders WHERE BiscuitType = 'Viennese Swirl'; output?

Reveal answer

102. It is the only Viennese Swirl order, and only the OrderID field is displayed.

Worked example 2: filtering by a number

Next, Jacques wants the bigger orders: those with a quantity of 6 or more.

SELECT OrderID, Quantity
FROM BiscuitOrders
WHERE Quantity >= 6;
Quantity ≥ 6Quantity below 6
BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
Output: orders 101 and 103
OrderIDQuantity
1016
1038

Order 101 is included because 6 is equal to 6, and >= means “greater than or equal to”. Orders 102 (4) and 104 (2) are less than 6, so they are left out.

Comparison operators you can use in WHERE
OperatorMeaningExample on QuantityMatching orders
=Equal toQuantity = 6101
>Greater thanQuantity > 6103
>=Greater than or equal toQuantity >= 6101, 103
<Less thanQuantity < 6102, 104
<=Less than or equal toQuantity <= 6101, 102, 104
<>Not equal toQuantity <> 6102, 103, 104

Why 'Hobnob' has quotation marks but 6 does not

Text value: 'Hobnob'

BiscuitType is a text field, so the value goes in quotation marks. Without them, the database would read Hobnob as the name of a field and the query would fail.

Number value: 6

Quantity is an integer field, so 6 is written as a plain number. That way it is compared as a value: 8 is bigger than 6, and 4 is smaller.

We use single quotation marks, which is standard SQL. Some resources, including our Knowledge Library, use double quotation marks for text. Cambridge accepts either in the exam, so just use one style consistently.

Check your understanding 2

How many records would WHERE Quantity > 6 return, compared with WHERE Quantity >= 6?

Reveal answer

> 6 returns 1 record (order 103, quantity 8). >= 6 returns 2 records (101 and 103), because it also includes quantities equal to 6.

Common Cambridge exam mistakes

✗ WHERE BiscuitType = Hobnob

Text values must go in quotation marks: 'Hobnob'. Without them, Hobnob is treated as a field name.

✗ Using > when the question says “6 or more”

“6 or more” includes 6, so it needs >=. Writing Quantity > 6 would wrongly leave out order 101.

✗ “BiscuitType must be in SELECT to use it in WHERE.”

It doesn't. WHERE can test any field in the table, whether or not that field is displayed.

WHERE works with SUM and COUNT too

The same filtering idea powers the SQL functions. Add WHERE BiscuitType = 'Hobnob' to a SUM query and it totals only the Hobnob quantities, giving 16 (see SQL SUM Explained). Add it to a COUNT query and it counts only the Hobnob orders, giving 3 (see SQL COUNT Explained).

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 display the OrderID of every order with a Quantity less than 5.

Reveal model answer
SELECT OrderID
FROM BiscuitOrders
WHERE Quantity < 5;

1 mark for SELECT OrderID, 1 mark for FROM BiscuitOrders and 1 mark for WHERE Quantity < 5. (The output is 102 and 104.)

Question 2 (2 marks)

A student writes this query. Identify the error and write the corrected WHERE clause.

SELECT OrderID, Quantity
FROM BiscuitOrders
WHERE BiscuitType = Viennese Swirl;
Reveal model answer

The text value is not in quotation marks (1).

WHERE BiscuitType = 'Viennese Swirl';

(1) The corrected query outputs order 102 with quantity 4.

Quick quiz

  1. Does WHERE choose rows or columns?
  2. Can WHERE test a field that is not listed after SELECT?
  3. What does >= mean?
  4. Which orders match WHERE Quantity >= 6?

Quiz answers

Reveal all answers
  1. Rows (records).
  2. Yes.
  3. Greater than or equal to.
  4. 101 and 103.

Final summary

WHERE

Keeps only the records that meet the condition.

Hidden test field

You can filter on BiscuitType without displaying it.

Text

Goes in quotation marks: 'Hobnob'.

Numbers

Written plainly: Quantity >= 6.

WHERE tests every record. Only the matches make it to the output.

Ready to sort the results?

WHERE chooses which records come back. ORDER BY decides the order they appear in.