SQL BETWEEN Operator

The SQL BETWEEN Operator

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: the begin and end values are included in the result.


Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Demo Database

Below is a selection from the Customers table:

Customers Table:

CustomerID CustomerName Country
1 Alfreds Futterkiste Germany
2 Ana Trujillo Emparedados Mexico
3 Antonio Moreno Taquería Mexico
4 Around the Horn UK
5 Berglunds snabbköp Sweden

Example 1: BETWEEN with Numbers

The following SQL statement selects all customers with a CustomerID between 1 and 3.

BETWEEN Example

SELECT * FROM Customers
WHERE CustomerID BETWEEN 1 AND 3;

This is equivalent to:

SELECT * FROM Customers
WHERE CustomerID >= 1 AND CustomerID <= 3;

Example 2: NOT BETWEEN

To select products outside this range, you can use NOT BETWEEN.

NOT BETWEEN Example

SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;

Example 3: BETWEEN with Text Values

The following SQL statement selects all products with a ProductName alphabetically between 'C' and 'M'.

BETWEEN with Text Example

SELECT * FROM Products
WHERE ProductName BETWEEN 'Chai' AND 'Mascot';

Example 4: BETWEEN with Dates

The BETWEEN operator works with dates as well. The following SQL statement selects all orders placed between '01-July-1996' and '31-July-1996'.

SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';