Either Condition with OR
With AND, a row must meet all the conditions. With OR, one of them is enough: the row gets into the result when at least one of the conditions is true for it. It doesn't matter which one, and it can be both. The racing and the sports games:
SELECT title, genre
FROM games
WHERE genre = 'Racing'
OR genre = 'Sports'
ORDER BY title;| title | genre |
|---|---|
| Goal Rush | Sports |
| Ice Hockey Pro | Sports |
| Pixel Racer | Racing |
| Turbo Karts | Racing |
Notice the “and” in “the racing and the sports games”. You want both kinds of games, but no game is a racing game and a sports game at the same time, so genre = 'Racing' AND genre = 'Sports' would find nothing. When you turn a request into SQL, think about one row: must it meet both conditions, or is one enough? Here, one is enough, so it's OR.
The conditions can be about different columns. The games that are very cheap or for adults only:
SELECT title, price, age_rating
FROM games
WHERE price < 10
OR age_rating = 18
ORDER BY title;| title | price | age_rating |
|---|---|---|
| Cube Logic | 5 | 3 |
| Zombie Town | 30 | 18 |
Typical mistake: the column written only once. This query isn't an error, but it finds only the racing games:
SELECT title, genre
FROM games
WHERE genre = 'Racing' OR 'Sports'
ORDER BY title;| title | genre |
|---|---|
| Pixel Racer | Racing |
| Turbo Karts | Racing |
OR joins two complete conditions, and 'Sports' alone isn't a condition, just a text. SQLite takes such a text for false, so only the first condition is left. Write the column in each condition: genre = 'Racing' OR genre = 'Sports'.
Instructions
The query in the editor should show the customers from London or Leeds, but it shows only those from London. Fix it. The result is sorted by name and starts like this:
| name | city |
|---|---|
| Emma Clark | London |
| Grace Hall | London |
| Isla Young | Leeds |
| … | … |
Below it, show the title and the price of the games that cost less than $15 or more than $45, sorted by price:
| title | price |
|---|---|
| Cube Logic | 5 |
| Word Quest | 10 |
| Goal Rush | 50 |
| Knight's Oath | 60 |
Start programming for free
5/11