Back to subject
MySQL/Comparison Operators

Express a Precise Range

Practical lesson

Let's read a BETWEEN query and confirm exactly which values it includes.

SELECT * FROM students WHERE age BETWEEN 15 AND 16; keeps every row where age falls anywhere from 15 to 16, including both endpoints themselves. Ali, at exactly 16, is included this is the key detail that separates BETWEEN from a strict < comparison.

Sara, at 17, falls outside this range and is correctly excluded from the result, since 17 is above the upper boundary of 16.

Written without BETWEEN, this exact same condition would be WHERE age >= 15 AND age <= 16; both forms are equivalent, but BETWEEN is generally considered more readable once a range has both a clear lower and upper bound.

Try mentally changing BETWEEN 15 AND 16 to BETWEEN 15 AND 17 Sara would then be included too. Getting comfortable predicting exactly which rows a condition will include, before running it, is a skill that pays off constantly as your queries grow more complex.

Try it yourself
Code

Starter example

Code
SELECT * FROM students WHERE age BETWEEN 15 AND 16;
Live preview
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1  | Ali  | 16  |
+----+------+-----+
1 row in set