TExcelTAD
Home › SQL › SQL BETWEEN

SQL BETWEEN

BETWEEN selects values within a range. Both ends are inclusive.

sql
SELECT columns
FROM table_name
WHERE column BETWEEN low AND high;

x BETWEEN 5 AND 10 means exactly x >= 5 AND x <= 10.

Numbers

sql · try it yourself
SELECT name, price
FROM products
WHERE price BETWEEN 5 AND 10
ORDER BY price;
name             | price
-----------------+------
Sourdough Loaf   | 5.5
Salted Almonds   | 5.99
Butter Croissant | 6.25
Swiss Cheese     | 7.8
Green Tea        | 8.75
USB-C Cable      | 9.99
(6 rows)

Products priced exactly 5 or 10 would be included too.

NOT BETWEEN

sql · try it yourself
SELECT first_name, last_name, salary
FROM employees
WHERE salary NOT BETWEEN 55000 AND 100000
ORDER BY salary;
first_name | last_name | salary
-----------+-----------+-------
Hana       | Sato      | 51000
Marco      | Ricci     | 54000
Grace      | Holt      | 185000
(3 rows)

Dates

ISO date strings compare in calendar order, so BETWEEN works nicely for date ranges. Orders placed in the second quarter of 2025:

sql · try it yourself
SELECT order_id, order_date, status
FROM orders
WHERE order_date BETWEEN '2025-04-01' AND '2025-06-30'
ORDER BY order_date, order_id;
order_id | order_date | status
---------+------------+----------
14       | 2025-04-03 | delivered
16       | 2025-04-04 | shipped
13       | 2025-04-08 | delivered
15       | 2025-04-23 | cancelled
19       | 2025-05-09 | shipped
18       | 2025-05-11 | cancelled
20       | 2025-05-15 | delivered
17       | 2025-05-25 | delivered
22       | 2025-06-06 | delivered
23       | 2025-06-12 | shipped
21       | 2025-06-13 | delivered
24       | 2025-06-19 | shipped
(12 rows)

Note: If a column stores date and time ('2025-06-30 14:00'), then BETWEEN '2025-04-01' AND '2025-06-30' misses everything after midnight on June 30. A safer pattern for timestamps is ts >= '2025-04-01' AND ts < '2025-07-01'.

Text

With text, BETWEEN uses alphabetical order. Customers whose name starts with A to D:

sql · try it yourself
SELECT name
FROM customers
WHERE name BETWEEN 'A' AND 'E'
ORDER BY name;
name
-------------
Amara Costa
Arjun Sharma
Bright Bytes
Code & Coffee
Corner Cafe
Daily Bread
Diego Ivanova
(7 rows)

We used 'E' as the upper bound because 'Diego…' is greater than 'D' — 'D' alone would exclude every name starting with D except the single letter "D".

Low value first

The smaller value must come first. A reversed range is valid SQL but matches nothing:

sql · try it yourself
SELECT COUNT(*) AS reversed FROM products WHERE price BETWEEN 10 AND 5;
reversed
--------
0
(1 row)

Combining with other conditions

sql · try it yourself
SELECT name, category_id, price
FROM products
WHERE price BETWEEN 20 AND 50
  AND category_id IN (7, 8)
ORDER BY price;
name                   | category_id | price
-----------------------+-------------+------
The SQL Handbook       | 8           | 29
Python Recipes         | 8           | 34.5
Cast Iron Pan          | 7           | 38
Learn Rust in Practice | 8           | 39
Chef Knife             | 7           | 45
(5 rows)

Test yourself

Is the value 10 included by price BETWEEN 5 AND 10?

  1. Yes
  2. No
  3. Only for integers
  4. Depends on the database

What does price BETWEEN 10 AND 5 return?

  1. Prices 5 to 10
  2. No rows
  3. An error
  4. All rows