SQL BETWEEN
The BETWEEN
operator in SQL is used to filter the result set within a specific range. It is often used in WHERE
clauses to match values that fall within a given range.
Syntax
The basic syntax of the BETWEEN
operator is:
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Examples
Here are some examples of using the BETWEEN
operator:
-- Select all employees with salaries between 40000 and 60000
SELECT * FROM Employees
WHERE Salary BETWEEN 40000 AND 60000;
-- Select all orders placed between January 1, 2022 and December 31, 2022
SELECT * FROM Orders
WHERE OrderDate BETWEEN '2022-01-01' AND '2022-12-31';
-- Select products with prices between 20 and 50
SELECT * FROM Products
WHERE Price BETWEEN 20 AND 50;