
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.
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
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, Quantitychooses the fields to display.FROM BiscuitOrdersnames the table.WHERE BiscuitType = 'Hobnob'keeps only the records whose BiscuitType is Hobnob.
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
| OrderID | Quantity |
|---|---|
| 101 | 6 |
| 103 | 8 |
| 104 | 2 |
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.
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;
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
| OrderID | Quantity |
|---|---|
| 101 | 6 |
| 103 | 8 |
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.
| Operator | Meaning | Example on Quantity | Matching orders |
|---|---|---|---|
= | Equal to | Quantity = 6 | 101 |
> | Greater than | Quantity > 6 | 103 |
>= | Greater than or equal to | Quantity >= 6 | 101, 103 |
< | Less than | Quantity < 6 | 102, 104 |
<= | Less than or equal to | Quantity <= 6 | 101, 102, 104 |
<> | Not equal to | Quantity <> 6 | 102, 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 = HobnobText values must go in quotation marks: 'Hobnob'. Without them, Hobnob is treated as a field name.
> when the question says “6 or more”“6 or more” includes 6, so it needs >=. Writing Quantity > 6 would wrongly leave out order 101.
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
- Does WHERE choose rows or columns?
- Can WHERE test a field that is not listed after SELECT?
- What does
>=mean? - Which orders match
WHERE Quantity >= 6?
Quiz answers
Reveal all answers
- Rows (records).
- Yes.
- Greater than or equal to.
- 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.