![]() |
VOOZH | about |
The SQL BETWEEN operator is used to retrieve values that fall within a specified range. It works with numbers, dates, and text and makes range-based filtering simple and readable.
Example: First, we create a demo SQL database and table, on which we will use the BETWEEN Operator command.
Query:
SELECT * FROM Employees
WHERE Age BETWEEN 26 AND 35;Output:
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;To understand the SQL BETWEEN Operator we use the below below employees table with the various examples and their output.
Find employees whose last names are not alphabetically between 'B' and 'S'.
SELECT FirstName, LastName
FROM Employees
WHERE LastName NOT BETWEEN 'B' AND 'S';Find employees hired between January 1, 2020 and December 31, 2021.
SELECT FirstName, LastName, HireDate
FROM Employees
WHERE HireDate BETWEEN '2020-01-01' AND '2021-12-31';BETWEEN operator is used to filter the HireDate field, returning records within the specified date range.Find employees whose age is not between 30 and 40.
SELECT FirstName, LastName, Age
FROM Employees
WHERE Age NOT BETWEEN 30 AND 40;Find employees whose salaries are between 50,000 and 70,000 and whose first names are either 'John', 'Sue', or 'Tom'.
SELECT FirstName, LastName, Salary
FROM Employees
WHERE Salary BETWEEN 50000 AND 70000
AND FirstName IN ('John', 'Sue', 'Tom');