Sub topic 7
Filtering Ranges with `BETWEEN`
Prerequisites and the basic rule
Assume you know basic SELECT statements, tables, columns, comparison operators such as =, <, and >, and the use of AND and OR in a WHERE clause.
BETWEEN tests whether a value falls inside an inclusive range. Inclusive means that the lower and upper endpoints are both included.
SELECT id, name, salary
FROM employees
WHERE salary BETWEEN 50000 AND 70000;This query returns employees whose salary is at least 50000 and at most 70000. It is equivalent to:
SELECT id, name, salary
FROM employees
WHERE salary >= 50000
AND salary <= 70000;The lower value must appear first. BETWEEN 70000 AND 50000 does not automatically reverse the endpoints. It normally matches no values because the requested range is logically empty.
| Expression | Equivalent comparison |
|---|---|
| `salary BETWEEN 50000 AND 70000` | `salary >= 50000 AND salary <= 70000` |
Choosing inclusive or exclusive boundaries
Use BETWEEN when both boundaries belong in the result. This is common for numeric values such as prices, scores, and ages.
SELECT product_id, product_name, price
FROM products
WHERE price BETWEEN 10.00 AND 25.00;A product priced at exactly 10.00 or exactly 25.00 is included.
If one endpoint should be excluded, write the comparisons explicitly:
SELECT product_id, product_name, price
FROM products
WHERE price >= 10.00
AND price < 25.00;This returns prices from 10.00 up to, but not including, 25.00.
Use explicit comparisons when the boundary rule is important or when the range represents adjacent intervals. For example, these ranges do not overlap:
SELECT order_id, total
FROM orders
WHERE total >= 100
AND total < 200;A value of 200 belongs to the next interval, rather than both intervals.
Dates, times, and precision
BETWEEN also works with date and timestamp values, but timestamps make the inclusive upper boundary easy to misunderstand. A condition such as this includes the exact timestamp at midnight on April 1, but not the rest of that day:
SELECT order_id, created_at
FROM orders
WHERE created_at BETWEEN '2026-03-01' AND '2026-04-01';If created_at stores timestamps, the upper value usually means 2026-04-01 00:00:00. It does not mean “the end of April 1.” For a full calendar period, use a lower-inclusive, upper-exclusive range:
SELECT order_id, created_at
FROM orders
WHERE created_at >= '2026-03-01'
AND created_at < '2026-04-01';This includes every timestamp in March and excludes every timestamp on or after April 1. It also avoids guessing the final representable time of a day, which differs between data types and database systems.
NULL and range predicates
NULL means that a value is missing or unknown. If the tested column is NULL, a BETWEEN condition does not evaluate to true. The row is therefore removed by the WHERE clause.
SELECT id, name, salary
FROM employees
WHERE salary BETWEEN 50000 AND 70000;This query excludes employees whose salary is NULL. To find missing salaries, test for NULL separately with IS NULL.
Index-friendly ranges
A comparison against the column directly can often use an index on that column:
SELECT order_id, created_at
FROM orders
WHERE created_at >= '2026-03-01'
AND created_at < '2026-04-01';An index is a data structure that helps the database locate rows without scanning every row. Applying a function to the indexed column, such as DATE(created_at), can prevent ordinary index use in many systems. The exact plan depends on the database and its indexes, so inspect the execution plan when performance matters.
Exercise
Does price BETWEEN 20 AND 30 include products priced at exactly 20 and exactly 30? What condition expresses a range that includes 20 but excludes 30?
Show answer
Yes, both endpoints are included. Use price >= 20 AND price < 30 for a lower-inclusive, upper-exclusive range.