In SQL Server database, the BETWEEN
operator is used to filter results based on a range of values.
BETWEEN Syntax:
The basic syntax for using the BETWEEN operator is as follows:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2
In this example, the query retrieves all rows from the "table_name" table where the values in the "column_name" column are between "value1" and "value2".
The BETWEEN
operator is inclusive, meaning that it will include both "value1" and "value2" in the results.
For example, the following query retrieves all rows from the "orders" table where the "order_date" column is between '2022-01-01' and '2022-12-31':
SELECT *
FROM orders
WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31';
BETWEEN example:
Employee table:
EMPLOYEE_ID | NAME | DEP_ID |
---|---|---|
1 | John | 33 |
2 | Samantha | 34 |
3 | Bill | 35 |
4 | James | 36 |
5 | Sandra | 37 |
SELECT * FROM employee
WHERE dep_id BETWEEN 34 AND 36;
BETWEEN result:
2 | Samantha | 34 |
3 | Bill | 35 |
4 | James | 36 |