TopicsCheat sheetLessons
Lurndo › Topics › IN

Learn SQL: IN

A list with IN

"Members from Leuven, Gent or Hasselt get a flyer," says Vic. Typing `OR` three times works. But it can be shorter, and easier to read.

IN is a short OR

With IN you test whether a value is in a list. town IN ('Leuven', 'Gent', 'Hasselt') means the same as three conditions joined with OR, but it's shorter.

Each value in the list gets its own quotes, separated by commas.

SELECT name, town FROM members WHERE town IN ('Leuven', 'Gent', 'Hasselt')
Result
nametown
Ine ClaesLeuven
Marc DierckxHasselt
Leila AmraniGent
Hanne De SmetLeuven
Lucas GoossensGent
Eva MichielsHasselt
Nora WillemsLeuven

NOT IN and numbers

NOT IN does the opposite: everything except the values in the list. Numbers go in the list without quotes.

This query shows the rentals of every film except films 1, 22 and 23.

SELECT * FROM rentals WHERE film_id NOT IN (1, 22, 23)
Result
idfilm_idmember_idrented_onreturned_onfine
2672024-10-012024-10-020
31912024-10-022024-10-084.5
5792024-10-032024-10-050
612122024-10-042024-10-126
71062024-10-052024-10-060
821152024-10-052024-10-070
9352024-10-062024-10-070
1117102024-10-082024-10-090

Try it yourself

Vic is filling a shop window with films from 1994, 2001 and 2023. Show title and year.

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
← PreviousCombining conditionsNext →From … to with BETWEEN
SQL topicsCheat sheetExcelLeaderboardAccountPrivacyNederlands