TopicsCheat sheetLessons
Lurndo › Topics › AND, OR and NOT

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
Result
titlegenreyear
Shrekanimation2001
Spirited Awayanimation2001
Finding Nemoanimation2003

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
Result
titlegenremin_age
Pulp Fictioncrime16
De zaak Alzheimercrime16
Alienhorror16
The Shininghorror16
Rundskopcrime16

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
← PreviousGreater, smaller, not equalNext →A list with IN
SQL topicsCheat sheetExcelLeaderboardAccountPrivacyNederlands