Introduction
SQL Server BETWEEN operator is used to filter records within a specified range of values. It is commonly used with the WHERE clause to retrieve rows whose column values fall between a lower boundary and an upper boundary.
The BETWEEN operator works with numbers, dates, and text values. It includes both boundary values, meaning the minimum and maximum values are part of the selected range.
SQL Server BETWEEN Operator Syntax
The basic syntax of the BETWEEN operator is:
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN lower_value AND upper_value;
Parameters:
column_name: The column whose values you want to filter.lower_value: The starting value of the range.upper_value: The ending value of the range.BETWEEN: Checks whether a value falls within the specified range.AND: Separates the lower and upper boundaries.
The condition returns rows where the column value is greater than or equal to the lower boundary and less than or equal to the upper boundary.
Sample Table
Consider an Employees table with the following data:
| EmployeeID | EmployeeName | Salary | Age |
|---|---|---|---|
| 1 | John | 25000 | 22 |
| 2 | Sarah | 35000 | 25 |
| 3 | Michael | 45000 | 30 |
| 4 | David | 55000 | 35 |
| 5 | Emma | 65000 | 40 |
We will use this table to understand different examples of the BETWEEN operator.
SQL Server BETWEEN with Numeric Values
You can use BETWEEN to filter numeric values within a specified range.
Example:
SELECT EmployeeID, EmployeeName, Salary
FROM Employees
WHERE Salary BETWEEN 30000 AND 55000;
Output:
| EmployeeID | EmployeeName | Salary |
|---|---|---|
| 2 | Sarah | 35000 |
| 3 | Michael | 45000 |
| 4 | David | 55000 |
Explanation:
The query returns employees whose salaries are between 30000 and 55000. Both 30000 and 55000 are included in the range. Therefore, an employee with a salary of exactly 55000 is also returned.
SQL Server BETWEEN with the AND Operator
The BETWEEN operator uses the AND keyword to define the lower and upper boundaries.
For example:
SELECT *
FROM Employees
WHERE Age BETWEEN 25 AND 35;
This query returns employees whose ages are 25, 30, or 35.
The following condition is logically equivalent:
SELECT *
FROM Employees
WHERE Age >= 25 AND Age <= 35;
Both queries produce the same result. However, BETWEEN can make range conditions easier to read.
SQL Server BETWEEN with Date Values
The BETWEEN operator can also filter records based on date values.
Suppose you have an Orders table:
| OrderID | CustomerName | OrderDate |
|---|---|---|
| 101 | John | 2026-01-05 |
| 102 | Sarah | 2026-01-15 |
| 103 | David | 2026-02-10 |
| 104 | Emma | 2026-03-20 |
To retrieve orders placed between January 1 and February 28, 2026, you can write:
SELECT OrderID, CustomerName, OrderDate
FROM Orders
WHERE OrderDate BETWEEN '2026-01-01' AND '2026-02-28';
This query returns orders within the specified date range, including the starting and ending dates.
Important: If OrderDate is a datetime or datetime2 column, the ending date literal represents midnight at the beginning of that day. Rows later on February 28 may be excluded.
For a complete date range with time values, use an exclusive upper boundary:
SELECT OrderID, CustomerName, OrderDate
FROM Orders
WHERE OrderDate >= '2026-01-01'
AND OrderDate < '2026-03-01';
This approach includes every time on February 28 and is generally recommended for filtering datetime columns by calendar dates.
SQL Server BETWEEN with String Values
You can use BETWEEN with character-based columns to filter text values within a range.
Example:
SELECT EmployeeID, EmployeeName
FROM Employees
WHERE EmployeeName BETWEEN 'A' AND 'M';
The comparison follows the collation rules of the column or expression. These rules determine how text values are sorted and compared, including case and accent sensitivity.
Therefore, string ranges should be used carefully. They do not always behave like simple alphabetical ranges based on the first character.
SQL Server NOT BETWEEN Operator
The NOT BETWEEN operator returns rows whose values fall outside the specified range.
Syntax:
SELECT column1, column2
FROM table_name
WHERE column_name NOT BETWEEN lower_value AND upper_value;
Example:
SELECT EmployeeID, EmployeeName, Salary
FROM Employees
WHERE Salary NOT BETWEEN 30000 AND 55000;
Output:
| EmployeeID | EmployeeName | Salary |
|---|---|---|
| 1 | John | 25000 |
| 5 | Emma | 65000 |
Explanation:
The query returns employees whose salaries are below 30000 or above 55000. Values equal to either boundary are excluded because NOT BETWEEN is the opposite of the inclusive BETWEEN condition.
SQL Server BETWEEN with Multiple Conditions
You can combine BETWEEN with other conditions using AND and OR.
Example using AND:
SELECT EmployeeID, EmployeeName, Salary, Age
FROM Employees
WHERE Salary BETWEEN 30000 AND 60000
AND Age BETWEEN 25 AND 35;
This query returns employees whose salaries are between 30000 and 60000 and whose ages are between 25 and 35.
Example using OR:
SELECT EmployeeID, EmployeeName, Salary
FROM Employees
WHERE Salary BETWEEN 20000 AND 30000
OR Salary BETWEEN 60000 AND 70000;
This query retrieves employees whose salaries fall within either of the two specified ranges.
Use parentheses when combining several AND and OR conditions to make the intended logic clear.
SQL Server BETWEEN with Decimal Values
The BETWEEN operator also supports decimal and numeric data types.
Example:
SELECT ProductID, ProductName, Price
FROM Products
WHERE Price BETWEEN 100.50 AND 500.75;
This query returns products with prices from 100.50 through 500.75, including both boundaries.
The comparison uses the underlying data type and its precision. If the column is a decimal, SQL Server compares the stored decimal values rather than rounding them automatically.
SQL Server BETWEEN with NOT NULL Values
SQL Server uses three-valued logic when evaluating conditions involving NULL.
If the column value is NULL, the BETWEEN condition does not evaluate to TRUE. Therefore, that row is not returned by the WHERE clause.
Example:
SELECT EmployeeID, EmployeeName, Salary
FROM Employees
WHERE Salary BETWEEN 30000 AND 55000;
Employees with a NULL salary are not included in the result.
To find rows where the salary is missing, use IS NULL:
SELECT EmployeeID, EmployeeName, Salary
FROM Employees
WHERE Salary IS NULL;
You should not use BETWEEN to check for NULL values.
SQL Server BETWEEN vs Comparison Operators
You can write a range condition using either BETWEEN or comparison operators.
Using BETWEEN:
SELECT *
FROM Employees
WHERE Salary BETWEEN 30000 AND 55000;
Using comparison operators:
SELECT *
FROM Employees
WHERE Salary >= 30000
AND Salary <= 55000;
Both queries represent the same inclusive range condition.
| BETWEEN | Comparison Operators |
|---|---|
| Uses one range expression | Uses separate conditions |
| Includes both boundaries | Can include or exclude boundaries |
| Easy to read for simple ranges | More flexible for complex ranges |
Uses AND between boundaries |
Uses logical operators to combine conditions |
Choose the form that makes your query easiest to understand.
Performance Considerations
The BETWEEN operator does not automatically make a query slow. SQL Server’s query optimizer chooses an execution plan based on the table structure, indexes, statistics, and query conditions.
For frequently filtered columns, an appropriate index may help SQL Server locate matching rows efficiently.
For example:
CREATE INDEX IX_Employees_Salary
ON Employees (Salary);
A query using the indexed column can then be evaluated using an index seek when the optimizer determines that it is beneficial.
Avoid applying functions or conversions to an indexed column in the WHERE condition when a direct range comparison can express the same logic. Such expressions can make efficient index usage more difficult.
Always examine the actual execution plan and test performance with representative data before creating indexes in a production database.
Practical Example
Consider an online store that needs to find products priced between 500 and 2000.
SELECT ProductID, ProductName, Price
FROM Products
WHERE Price BETWEEN 500 AND 2000
ORDER BY Price ASC;
This query returns products priced from 500 through 2000 and sorts them from the lowest price to the highest price.
You can also filter orders placed during a specific month:
SELECT OrderID, CustomerID, OrderDate, TotalAmount
FROM Orders
WHERE OrderDate >= '2026-11-01'
AND OrderDate < '2026-12-01'
ORDER BY OrderDate;
This second query is particularly useful when OrderDate contains both date and time information.
The BETWEEN operator is a convenient way to express inclusive ranges, while explicit comparison operators are often preferable when working with datetime boundaries or exclusive ranges.