🎯 Syllabus & Goals 3 min
Cambridge 9.1 · SQL scripts to query data Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Combine conditions with
AND,ORandNOT, using brackets where needed. - Use
BETWEENfor a range,INfor a list andLIKEfor a pattern. - Predict the exact output of a query that uses these operators.
Textbook: Chapter 9, §9.1.4 (pp. 346–348)
Recap / Warm-Up 5 min
Last lesson: SELECT picks fields, FROM the table, WHERE filters rows, ORDER BY sorts. Today's table is FILM again.
| FilmID | Title | Genre | ReleaseYear | Minutes | Price | Copies |
|---|---|---|---|---|---|---|
| F01 | Ocean Deep | Documentary | 2021 | 95 | 3.99 | 4 |
| F02 | Robot Rescue | Animation | 2019 | 88 | 2.99 | 6 |
| F03 | The Last Orbit | Sci-Fi | 2023 | 132 | 5.49 | 3 |
| F04 | Laugh Out Loud | Comedy | 2018 | 101 | 1.99 | 5 |
| F05 | Storm Chasers | Action | 2022 | 117 | 4.49 | 2 |
| F06 | Star Academy | Sci-Fi | 2020 | 124 | 3.49 | 5 |
| F07 | Pizza Party | Comedy | 2023 | 92 | 4.99 | 7 |
| F08 | Silent Forest | Drama | 2017 | 110 | 1.49 | 1 |
| F09 | Space Pups | Animation | 2022 | 84 | 3.99 | 8 |
| F10 | Second Chance | Drama | 2021 | 106 | 2.49 | 3 |
Quick starter
How many records does WHERE Copies >= 5 keep?
Reveal the answer
Five: F02 (6), F04 (5), F06 (5), F07 (7), F09 (8). >= includes 5 itself.
🧠 Key Concept 14 min
1 · AND, OR, NOT
| Operator | A record is kept when… |
|---|---|
AND | all the conditions are true |
OR | one or more of the conditions is true |
NOT | the condition after it is false |
AND narrows the result; OR widens it.AND — Sci-Fi films shorter than 130 minutes:
SELECT Title, Minutes FROM FILM WHERE Genre = 'Sci-Fi' AND Minutes < 130;
| Title | Minutes |
|---|---|
| Star Academy | 124 |
OR — dramas, or any film with 7 or more copies:
SELECT Title, Genre, Copies FROM FILM WHERE Genre = 'Drama' OR Copies >= 7;
| Title | Genre | Copies |
|---|---|---|
| Pizza Party | Comedy | 7 |
| Silent Forest | Drama | 1 |
| Space Pups | Animation | 8 |
| Second Chance | Drama | 3 |
2 · BETWEEN — a range
BETWEEN low AND high keeps values from low to high. Both ends are included.
>= and <= together.3 · IN — a list of values
Genre IN ('Animation', 'Comedy') is a short way to write Genre = 'Animation' OR Genre = 'Comedy'.
4 · LIKE — a pattern
LIKE matches text against a pattern built with wildcards:
% — any number of characters
'S%' starts with S · '%s' ends with s · '%Party%' contains "Party"
_ — exactly one character
'_a%' has "a" as its second letter · 'F__' is F plus exactly two characters
5 · NOT
NOT reverses the condition that follows it. WHERE NOT Genre = 'Comedy' gives the same rows as WHERE Genre <> 'Comedy'. It also combines with IN, LIKE and BETWEEN:
SELECT Title, Genre
FROM FILM
WHERE NOT Genre IN ('Comedy', 'Drama', 'Sci-Fi');| Title | Genre |
|---|---|
| Ocean Deep | Documentary |
| Robot Rescue | Animation |
| Storm Chasers | Action |
| Space Pups | Animation |
Worked Example 12 min
(a) BETWEEN — "films priced from 2.00 to 4.00"
SELECT Title, PriceFROM FILMWHERE Price BETWEEN 2.00 AND 4.00;Price is real, so no quotes. Both limits are included.- Test each price: 3.99 ✓, 2.99 ✓, 5.49 ✗, 1.99 ✗, 4.49 ✗, 3.49 ✓, 4.99 ✗, 1.49 ✗, 3.99 ✓, 2.49 ✓.Keep the ticks, in table order.
SELECT Title, Price FROM FILM WHERE Price BETWEEN 2.00 AND 4.00;
| FilmID | Title | Genre | ReleaseYear | Minutes | Price | Copies |
|---|---|---|---|---|---|---|
| F01 | Ocean Deep | Documentary | 2021 | 95 | 3.99 | 4 |
| F02 | Robot Rescue | Animation | 2019 | 88 | 2.99 | 6 |
| F03 | The Last Orbit | Sci-Fi | 2023 | 132 | 5.49 | 3 |
| F04 | Laugh Out Loud | Comedy | 2018 | 101 | 1.99 | 5 |
| F05 | Storm Chasers | Action | 2022 | 117 | 4.49 | 2 |
| F06 | Star Academy | Sci-Fi | 2020 | 124 | 3.49 | 5 |
| F07 | Pizza Party | Comedy | 2023 | 92 | 4.99 | 7 |
| F08 | Silent Forest | Drama | 2017 | 110 | 1.49 | 1 |
| F09 | Space Pups | Animation | 2022 | 84 | 3.99 | 8 |
| F10 | Second Chance | Drama | 2021 | 106 | 2.49 | 3 |
| Title | Price |
|---|---|
| Ocean Deep | 3.99 |
| Robot Rescue | 2.99 |
| Star Academy | 3.49 |
| Space Pups | 3.99 |
| Second Chance | 2.49 |
(b) LIKE — "films whose title starts with S"
SELECT FilmID, TitleFROM FILMWHERE Title LIKE 'S%';S must be first; % then matches whatever follows, even nothing.
SELECT FilmID, Title FROM FILM WHERE Title LIKE 'S%';
| FilmID | Title |
|---|---|
| F05 | Storm Chasers |
| F06 | Star Academy |
| F08 | Silent Forest |
| F09 | Space Pups |
| F10 | Second Chance |
(c) IN — "all animations and comedies"
SELECT Title, Genre
FROM FILM
WHERE Genre IN ('Animation', 'Comedy');| Title | Genre |
|---|---|
| Robot Rescue | Animation |
| Laugh Out Loud | Comedy |
| Pizza Party | Comedy |
| Space Pups | Animation |
(d) Why brackets matter
Goal: comedies or animations, but only those under 4.00. Compare these two queries.
Without brackets ✗
SELECT Title, Genre, Price FROM FILM WHERE Genre = 'Comedy' OR Genre = 'Animation' AND Price < 4.00;
| Title | Genre | Price |
|---|---|---|
| Robot Rescue | Animation | 2.99 |
| Laugh Out Loud | Comedy | 1.99 |
| Pizza Party | Comedy | 4.99 |
| Space Pups | Animation | 3.99 |
With brackets ✓
SELECT Title, Genre, Price FROM FILM WHERE (Genre = 'Comedy' OR Genre = 'Animation') AND Price < 4.00;
| Title | Genre | Price |
|---|---|---|
| Robot Rescue | Animation | 2.99 |
| Laugh Out Loud | Comedy | 1.99 |
| Space Pups | Animation | 3.99 |
- SQL does
ANDbeforeOR. So the first query means: Comedy, or (Animation and under 4.00).Pizza Party is a comedy, so it passes even though it costs 4.99. - Brackets force the
ORto be worked out first. The price test then applies to both genres.Pizza Party now fails the price test and drops out.
Try It Yourself 12 min
Goal: Show the output of SELECT Title FROM FILM WHERE Title LIKE '%Party%'; and of the same query with LIKE '_a%'.
Goal: Write a query to list the Title and ReleaseYear of films released from 2019 to 2021, oldest first. Show the output.
Goal: Rewrite WHERE Genre IN ('Drama', 'Action') AND NOT Price BETWEEN 2.00 AND 4.00 using only =, <, >, AND, OR and brackets. Check both versions give the same rows.
Hint
"Not between 2.00 and 4.00" means below 2.00 or above 4.00. Put brackets round each OR pair.
📝 Exam Practice 10 min
A running club records a fun run in the table RACE. Questions 1–3 use this table.
| RunnerID | FirstName | AgeGroup | Distance | TimeMins | Club |
|---|---|---|---|---|---|
| R01 | Sam | U14 | 5 | 24.5 | Hilltop |
| R02 | Alex | U16 | 10 | 51.2 | Lakeside |
| R03 | Jordan | U14 | 5 | 27.8 | Riverside |
| R04 | Casey | U16 | 5 | 22.9 | Lakeside |
| R05 | Morgan | U18 | 10 | 47.6 | Hilltop |
| R06 | Riley | U18 | 5 | 21.4 | Riverside |
| R07 | Jamie | U16 | 10 | 55.0 | Riverside |
| R08 | Taylor | U14 | 5 | 30.1 | Hilltop |
Show the output from this query.
SELECT FirstName, TimeMins FROM RACE WHERE Distance = 5 AND TimeMins < 25;
Mark scheme
- Sam 24.5 (1).
- Casey 22.9 (1).
- Riley 21.4 (1).
- Max 2 if any extra record or field is shown.
Show the output from this query.
SELECT RunnerID, FirstName
FROM RACE
WHERE Club IN ('Hilltop', 'Lakeside')
AND NOT AgeGroup = 'U14';Mark scheme
- R02 Alex (1).
- R04 Casey (1).
- R05 Morgan (1).
Hilltop/Lakeside runners are R01, R02, R04, R05, R08; NOT U14 removes R01 and R08.
Complete the query to show the first name and age group of runners with a time from 25 to 50 minutes, in alphabetical order of first name.
SELECT FirstName, ................ FROM ................ WHERE TimeMins ................ 25 ................ 50 ORDER BY ................;
Mark scheme
AgeGroup(1).RACE(1).BETWEEN…AND(1).FirstName(1).
| FirstName | AgeGroup |
|---|---|
| Jordan | U14 |
| Morgan | U18 |
| Taylor | U14 |
Recap & Key Terms 3 min
AND needs every condition true; OR needs at least one; NOT reverses one. Use brackets when mixing them. BETWEEN includes both ends, IN tests a list, LIKE matches a pattern.
- AND
- Specifies multiple conditions that must all be true.
- OR
- Specifies multiple conditions where one or more must be true.
- NOT
- Specifies a condition that must be false.
- BETWEEN
- Tests for a value in a range between two values, both included.
- IN
- Tests a field against a list of several values.
- LIKE
- Searches for a pattern;
%stands for any number of characters,_for exactly one.
Homework 1 min
Task (≤ 15 min): Using the RACE table, show the output of this query and explain in one sentence what '%e%' matches.
SELECT FirstName, Club FROM RACE WHERE FirstName LIKE '%e%' AND Distance = 10;
Model answer
| FirstName | Club |
|---|---|
| Alex | Lakeside |
| Jamie | Riverside |
'%e%' matches any name with an e anywhere in it (any characters before and after). Morgan also ran 10 km but has no e, so is left out.