Learn SQL: AND, OR and NOT
Combining conditions
Vic is putting together a theme week. "Animation, but not the old stuff. And for late nights: horror or crime." One condition isn't enough any more.
AND, OR and NOT
With AND, both conditions have to be true. With OR, one of the two is enough. NOT turns a condition around.
This query finds animated films from after 2000: they have to meet both conditions.
SELECT title, genre, year FROM films WHERE genre = 'animation' AND year > 2000| title | genre | year |
|---|---|---|
| Shrek | animation | 2001 |
| Spirited Away | animation | 2001 |
| Finding Nemo | animation | 2003 |
AND first, then OR
Watch out: SQL works out AND before OR, just as multiplying comes before adding. If you combine them, put brackets around what belongs together.
This query finds horror or crime, and of those only the films rated 16. Without the brackets, every horror film would get in, whatever the age.
SELECT title, genre, min_age FROM films WHERE (genre = 'horror' OR genre = 'crime') AND min_age = 16| title | genre | min_age |
|---|---|---|
| Pulp Fiction | crime | 16 |
| De zaak Alzheimer | crime | 16 |
| Alien | horror | 16 |
| The Shining | horror | 16 |
| Rundskop | crime | 16 |
Try it yourself
Vic is calling the premium members from Mechelen for a preview screening. Show their name and email.
Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson