When you need values within a range, BETWEEN is your friend. It is simpler than
writing age >= 20 AND age <= 25.
BETWEEN with Numbers
-- Students aged 20 to 23 inclusive
SELECT * FROM students WHERE age BETWEEN 20 AND 23;
BETWEEN is inclusive, meaning it includes both 20 and 23. Some people
forget this and it causes off-by-one bugs. Keep it in mind.
BETWEEN with Text
You can also use BETWEEN with text. It works alphabetically:
-- Students whose names fall between A and M alphabetically
SELECT * FROM students WHERE name BETWEEN 'A' AND 'M';
This will include names starting with A through M. But here is the catch — it compares character by character, so it might not always behave exactly how you expect. Test your queries to make sure.
NOT BETWEEN
-- Students NOT in the age range 20-23
SELECT * FROM students WHERE age NOT BETWEEN 20 AND 23;