We use cookies to enhance your experience on the site
CodeWorlds

Ranges: BETWEEN

You already know lists of values. Now you will learn a second elegant shortcut: the BETWEEN operator, which selects values from a range from-to.

BETWEEN - a from-to range

The BETWEEN operator selects values from a range, together with both ends:

1SELECT * FROM ksiazki
2WHERE rok_wydania BETWEEN 1900 AND 2000;

This means: "Books published from 1900 to 2000 inclusive". It is a convenient replacement for two comparison conditions:

1SELECT * FROM ksiazki
2WHERE rok_wydania >= 1900 AND rok_wydania <= 2000;

Both ends are included

Remember the most important thing, @name:

BETWEEN
always includes both ends of the range. The condition
BETWEEN 1900 AND 2000
covers both the year 1900 and the year 2000.

Great for numbers and prices

BETWEEN
works perfectly for years, prices or any numbers:

1SELECT * FROM ksiazki
2WHERE cena BETWEEN 20 AND 50;

This means: "Books priced from 20 to 50 coins inclusive". Thanks to

IN
and
BETWEEN
your queries will be shorter and more readable than the most beautifully written scroll.

Go to CodeWorlds