We use cookies to enhance your experience on the site
CodeWorlds

Combining many conditions in practice

You already know all the scribe's tools: comparisons,

AND
/
OR
/
NOT
,
IN
,
BETWEEN
,
LIKE
and
IS NULL
. Time to combine them in one powerful query. This is where the true power of the Scriptorium shows itself.

Two AND conditions

The simplest combination is two conditions joined with the

AND
operator. We can combine different tools - for example a
BETWEEN
range with a comparison:

1SELECT * FROM ksiazki
2WHERE rok_wydania BETWEEN 1990 AND 2020
3  AND dostepna = 1;

This means: "Books published in the years 1990-2020 AND available at the same time".

Combining different operators

Operators can be freely mixed. Here a price range meets a list of categories:

1SELECT * FROM ksiazki
2WHERE cena BETWEEN 20 AND 80
3  AND kategoria_id IN (1, 2);

This means: "Books priced from 20 to 80 coins, belonging to category 1 or 2".

Three conditions at once

We can join even three conditions - a range, a list and a pattern:

1SELECT * FROM ksiazki
2WHERE cena BETWEEN 20 AND 80
3  AND kategoria_id IN (1, 2)
4  AND tytul LIKE 'O%';

This query will return books priced from 20 to 80 coins, from category 1 or 2, whose title starts with O. Three conditions in one!

Readability matters

When there are many conditions, it is worth writing each one on a separate line and indenting them under

WHERE
. This way the query becomes as readable as a well-written scroll. Remember the parentheses too when you mix
AND
with
OR
. Practice combining, @name - this is the key to mastery!

Go to CodeWorlds