Careeroza
BETWEEN
The SQL BETWEEN Operator
BETWEEN selects values within a range. The values can be numbers, text or dates. Both ends are included.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;Numbers
Example
Products priced between 5 and 10:
SELECT ProductName, Price FROM Products
WHERE Price BETWEEN 5 AND 10;Result: 3 rows
| ProductName | Price |
|---|---|
| Green Tea 100 bags | 6.75 |
| Olive Oil 1L | 9.99 |
| Almonds 500g | 8.25 |
NOT BETWEEN
Example
Products priced outside the range 5 to 10:
SELECT ProductName, Price FROM Products
WHERE Price NOT BETWEEN 5 AND 10;Result: 3 rows
| ProductName | Price |
|---|---|
| Basmati Rice 5kg | 12.5 |
| Dark Chocolate | 3.49 |
| Espresso Beans 1kg | 18 |
Dates
Example
Orders placed in February 2024:
SELECT * FROM Orders
WHERE OrderDate BETWEEN '2024-02-01' AND '2024-02-29';Result: 2 rows
| OrderID | CustomerID | EmployeeID | OrderDate | Amount |
|---|---|---|---|---|
| 103 | 2 | 2 | 2024-02-03 | 412 |
| 104 | 1 | 3 | 2024-02-18 | 56.25 |
Note: If the column also stores a time (DATETIME), BETWEEN '2024-02-01' AND '2024-02-29' misses everything after midnight on the 29th. For date-times, prefer OrderDate >= '2024-02-01' AND OrderDate < '2024-03-01'.
Text
Example
Customer names alphabetically between B and E (E itself is excluded unless the name is exactly "E"):
SELECT CustomerName FROM Customers
WHERE CustomerName BETWEEN 'B' AND 'E'
ORDER BY CustomerName;Result: 3 rows
| CustomerName |
|---|
| Blue Harbor Foods |
| Cedar & Co |
| Delta Mart |