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.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
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 |
BETWEEN with NumbersThe following SQL statement selects all customers with a CustomerID between 1 and 3.
SELECT * FROM Customers WHERE CustomerID BETWEEN 1 AND 3;
This is equivalent to:
SELECT * FROM Customers
WHERE CustomerID >= 1 AND CustomerID <= 3;
NOT BETWEENTo select products outside this range, you can use NOT BETWEEN.
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
BETWEEN with Text ValuesThe following SQL statement selects all products with a ProductName alphabetically between 'C' and 'M'.
SELECT * FROM Products WHERE ProductName BETWEEN 'Chai' AND 'Mascot';
BETWEEN with DatesThe 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';