SQL AND, OR, IN and LIKE
Builds on lessons 1 to 3. Start at lesson 1
Learn
AND, OR and brackets
One condition is rarely enough. AND needs both sides to be true, OR needs at least one:
SELECT title FROM songs WHERE genre = 'pop' AND plays > 1000;
SELECT title FROM songs WHERE genre = 'pop' OR genre = 'rock';Here is the trap. AND is stronger than OR, the same way * beats + in maths. So this:
WHERE genre = 'pop' OR genre = 'rock' AND seconds < 200means "all pop songs, plus rock songs under 200 seconds". If you meant "short songs that are pop or rock", add brackets: (genre = 'pop' OR genre = 'rock') AND seconds < 200. When you mix AND and OR, just always use brackets. Future you says thanks.
Learn
IN, BETWEEN and LIKE
Long OR chains get tiring, so SQL has shortcuts:
genre IN ('pop', 'rock', 'indie')matches any value in the list.seconds BETWEEN 180 AND 240is a range, and both ends are included.NOTflips a condition:genre NOT IN ('pop').
For text patterns there is LIKE, with two wildcards:
%means "any number of characters, even none"_means "exactly one character"
WHERE title LIKE 'the%' -- starts with "the"
WHERE title LIKE '%love%' -- contains "love" anywhereIn SQLite, LIKE ignores upper and lower case for plain English letters, so '%love%' also finds Love Story. Other databases can be stricter, so don't build your whole personality around that.
GCSE exam boards
- OCR 2.2.3 Strings, files, arrays, sub programs, SQL
- AQA 3.7.2 SQL
Try
Run it: short pop or rock songs
The query in the editor runs against this lesson's tables (open Tables above the editor to see them). Run it, change the WHERE or the columns, run it again.
Tables songs
| id | title | artist | genre | seconds | plays |
|---|---|---|---|---|---|
| 1 | Lovesick Robot | The Toasters | pop | 198 | 5400 |
| 2 | Homework Can Wait | Lofi Owl | lofi | 152 | 12000 |
| 3 | Thunder Lunchbox | Gravel | rock | 254 | 870 |
| 4 | Love You Like WiFi | Kai Neon | pop | 231 | 9100 |
| 5 | The Final Boss | Pixel Choir | chiptune | 176 | 3300 |
| 6 | Monday Again | Gravel | rock | 189 | 2100 |
| 7 | Rainy Bus Window | Lofi Owl | lofi | 245 | 640 |
| 8 | Glow Up | Kai Neon | pop | 166 | 15000 |
| 9 | The Long Goodbye | Velvet Static | indie | 302 | 450 |
| 10 | Skate Park Sunset | Velvet Static | indie | 210 | 1800 |
Show the SQL that built them
CREATE TABLE songs (id INTEGER PRIMARY KEY, title TEXT, artist TEXT, genre TEXT, seconds INTEGER, plays INTEGER); INSERT INTO songs VALUES (1, 'Lovesick Robot', 'The Toasters', 'pop', 198, 5400), (2, 'Homework Can Wait', 'Lofi Owl', 'lofi', 152, 12000), (3, 'Thunder Lunchbox', 'Gravel', 'rock', 254, 870), (4, 'Love You Like WiFi', 'Kai Neon', 'pop', 231, 9100), (5, 'The Final Boss', 'Pixel Choir', 'chiptune', 176, 3300), (6, 'Monday Again', 'Gravel', 'rock', 189, 2100), (7, 'Rainy Bus Window', 'Lofi Owl', 'lofi', 245, 640), (8, 'Glow Up', 'Kai Neon', 'pop', 166, 15000), (9, 'The Long Goodbye', 'Velvet Static', 'indie', 302, 450), (10, 'Skate Park Sunset', 'Velvet Static', 'indie', 210, 1800);
-- Short songs that are pop or rock (brackets matter!) SELECT title, genre, seconds FROM songs WHERE (genre = 'pop' OR genre = 'rock') AND seconds < 200;
Practice
Filter by genre and length
Return the title and artist of every song whose genre is lofi or indie and that is between 150 and 250 seconds long (including exactly 150 and 250).
SELECT title, artist
FROM songs
WHERE genre IN ('lofi', 'indie');
Practice
Songs that start with The
Return the title of every song whose title starts with the word The.
SELECT title FROM songs WHERE title = 'The';
Break/fixFix the bug
A long pop song sneaks in
The playlist should have only songs under 200 seconds that are pop or lofi. A 231 second song shows up. Fix the condition.
SELECT title, genre, seconds FROM songs WHERE genre = 'pop' OR genre = 'lofi' AND seconds < 200;
PracticePredict the output
Both ends count
Which songs does this query return? Type one title per line, in the order they appear in the table.
SELECT title FROM songs WHERE seconds BETWEEN 176 AND 210;
Your turn
A playlist for revising
Build a revision playlist: the title and genre of every song that is not pop or rock, is under 4 minutes (240 seconds), and has at least 1000 plays.
On your own. Hints open after your first check.
-- Your query here
Passed
You can now