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;
Note: The order of the bounds matters. Always write the smaller value first and the larger value second. Writing BETWEEN 50 AND 20 defines an empty range and returns no rows in standard SQL.

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';
Note: When a date column also stores a time component, BETWEEN can miss rows late on the final day, because '2025-03-31' is treated as midnight at the start of that day. To capture the whole day, use a condition like order_date >= '2025-01-01' AND order_date < '2025-04-01' instead.

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;
ExpressionIncluded?Reason
price BETWEEN 20 AND 50, price = 20YesLower bound is inclusive
price BETWEEN 20 AND 50, price = 50YesUpper bound is inclusive
price BETWEEN 20 AND 50, price = 50.01NoAbove the upper bound
price NOT BETWEEN 20 AND 50, price = 10YesOutside the range
  • 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?