We use cookies to enhance your experience on the site
CodeWorlds

Lists of values: IN

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.

IN instead of many OR

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

kategoria_id
is 1 OR 2 OR 3". It is the exact equivalent of:

1SELECT * FROM ksiazki
2WHERE kategoria_id = 1 OR kategoria_id = 2 OR kategoria_id = 3;

but much shorter and more readable!

IN with text

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.

Numbers without quotes

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!

Go to CodeWorlds