The BETWEEN
operator in SQL is used to filter the result set within a certain range. It is inclusive, meaning it will include the boundary values specified.
The basic syntax of the BETWEEN
operator is:
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Here’s an example of how to use the BETWEEN
operator:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 50;
This query retrieves all products with a price between 10 and 50, including those priced exactly at 10 and 50.
The BETWEEN
operator can also be used to filter records based on date ranges:
SELECT * FROM Orders
WHERE OrderDate BETWEEN '2024-01-01' AND '2024-12-31';
This retrieves all orders placed in the year 2024.
While using BETWEEN
is straightforward, consider the following:
BETWEEN
are indexed for better performance.BETWEEN
against those using comparison operators.