When there are many
OR conditions, the query becomes long and hard to read. Luckily the Library gives us an elegant shortcut: the IN operator for a list of allowed values.Instead of writing many
OR conditions, we give a list of values in parentheses:1SELECT * FROM ksiazki
2WHERE kategoria_id IN (1, 2, 3);This means: "Books whose
is 1 OR 2 OR 3". It is the exact equivalent of:kategoria_id
1SELECT * FROM ksiazki
2WHERE kategoria_id = 1 OR kategoria_id = 2 OR kategoria_id = 3;but much shorter and more readable!
The
IN operator also works with text - then remember the single quotes around each value:1SELECT * FROM autorzy
2WHERE kraj IN ('Polska', 'Grecja', 'Egipt');This means: "Authors from Poland, Greece or Egypt". We separate the values inside the parentheses with commas.
When the list contains numbers, we do not use quotes:
1SELECT * FROM ksiazki
2WHERE kategoria_id IN (1, 3, 5);Remember, @name:
IN is your ally when you want to pick several specific values from one column. A shorter notation, fewer mistakes - the true art of the scribe!