SQL Between
The BETWEEN operator selects rows whose value falls within a range, including both endpoints.
Selecting a range of values
Range questions come up constantly: prices within a budget, orders placed during a given month, or ages inside a bracket. The BETWEEN operator expresses these ranges directly. It checks whether a column's value is greater than or equal to a lower bound and less than or equal to an upper bound, so both endpoints are included in the result.
Because it is inclusive, BETWEEN is a compact way to write a condition that would otherwise need two comparisons joined with AND. The two queries below are equivalent.
BETWEEN and its longhand equivalent
-- Using BETWEEN
SELECT product_name, price
FROM Products
WHERE price BETWEEN 20 AND 50;
-- The equivalent comparison version
SELECT product_name, price
FROM Products
WHERE price >= 20 AND price <= 50;Working with dates
BETWEEN is often used to filter by date. You supply the range as two date literals, and rows falling on or between those dates are returned. This makes reporting over a period straightforward.
Orders within a date range
SELECT order_id, order_date, order_total
FROM Orders
WHERE order_date BETWEEN '2025-01-01' AND '2025-03-31';Text ranges and NOT BETWEEN
BETWEEN also works with text, comparing values in the alphabetical order defined by the column's collation. Adding NOT reverses the condition so that only values outside the range are returned.
Exclude a numeric range with NOT BETWEEN
SELECT product_name, price
FROM Products
WHERE price NOT BETWEEN 20 AND 50;- BETWEEN includes both the lower and upper bounds.
- Always list the smaller value before the larger one.
- It works with numbers, dates, and text.
- NOT BETWEEN returns values that fall outside the range.
- For date-time columns, prefer a half-open range to avoid missing rows on the last day.
Exercise: SQL Between
What kind of comparison does BETWEEN perform?