The SQL BETWEEN operator is used to filter results within a specified range, including the start and end values. It can be applied to numeric, date, and text columns, providing an efficient way to retrieve data that falls within a certain range.
In this article, we will explore the SQL BETWEEN operator with examples.
SQL BETWEEN Operator
The SQL ‘BETWEEN ' operator is used to filter the result set within a specified range. It can be applied to numeric, date, and text columns. The BETWEEN operator is inclusive, meaning it includes the start and end values in the result set.
Syntax:
The basic syntax of the ‘BETWEEN ' the operator is as follows:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
Parameters:
- column_name(s): The name of the column(s) you want to retrieve data from.
- table_name: The name of the table containing the data.
- column_name: The name of the column you want to apply the
BETWEEN filter to.
- value1: The starting value of the range.
- value2: The ending value of the range.
Creating a Sample Table
Let’s create a sample table named Employees to demonstrate the ‘BETWEEN ' operator.
Creating the Table
CREATE TABLE Employees ( EmployeeID INT, FirstName VARCHAR(50), LastName VARCHAR(50), Age INT, Salary DECIMAL(10, 2), HireDate DATE );
Inserting Sample Data
INSERT INTO Employees (EmployeeID, FirstName, LastName, Age, Salary, HireDate) VALUES (1, 'John', 'Doe', 28, 50000, '2021-01-15'), (2, 'Jane', 'Smith', 34, 60000, '2020-03-22'), (3, 'Sam', 'Brown', 45, 75000, '2018-07-10'), (4, 'Sue', 'Wilson', 25, 48000, '2021-10-01'), (5, 'Tom', 'Harris', 38, 68000, '2019-05-12');
Output:
EmployeeID |
FirstName |
LastName |
Age |
Salary |
HireDate |
1 |
John |
Doe |
28 |
50000 |
2021-01-15 |
2 |
Jane |
Smith |
34 |
60000 |
2020-03-22 |
3 |
Sam |
Brown |
45 |
75000 |
2018-07-10 |
4 |
Sue |
Wilson |
25 |
48000 |
2021-10-01 |
5 |
Tom |
Harris |
38 |
68000 |
2019-05-12 |
Using the BETWEEN Operator
Example 1: Filtering Numeric Values
To find employees whose ages are between 30 and 40:
SELECT EmployeeID, FirstName, LastName, Age FROM Employees WHERE Age BETWEEN 30 AND 40;
Output:
EmployeeID |
FirstName |
LastName |
Age |
2 |
Jane |
Smith |
34 |
5 |
Tom |
Harris |
38 |
Explanation: This query returns employees aged between 30 and 40, inclusive.
Example 2: Filtering Dates
To find employees hired between ‘2020-01-01‘ and ‘2021-12-31‘:
SELECT EmployeeID, FirstName, LastName, HireDate FROM Employees WHERE HireDate BETWEEN '2020-01-01' AND '2021-12-31';
Output:
EmployeeID |
FirstName |
LastName |
HireDate |
1 |
John |
Doe |
2021-01-15 |
2 |
Jane |
Smith |
2020-03-22 |
4 |
Sue |
Wilson |
2021-10-01 |
Explanation: This query returns employees hired between January 1, 2020, and December 31, 2021, inclusive.
Example 3: Filtering Text Values
To find employees whose last names are alphabetically between ‘Brown‘ and ‘Smith‘:
SELECT EmployeeID, FirstName, LastName FROM Employees WHERE LastName BETWEEN 'Brown' AND 'Smith';
Output:
EmployeeID |
FirstName |
LastName |
3 |
Sam |
Brown |
2 |
Jane |
Smith |
Explanation: This query returns employees whose last names fall between ‘Brown‘ and ‘Smith‘ alphabetically.
Conclusion
The BETWEEN operator is a powerful and easy-to-use tool for filtering result sets within a specified range. It works with numeric, date, and text data types, making it versatile for various query needs. Understanding how to use the BETWEEN operator effectively can help you retrieve precise and relevant data from your SQL databases.
By using the examples provided, you can see how BETWEEN can simplify your SQL queries and make your data management tasks more efficient.
FAQs on SQL BETWEEN Operator
Why do we use the SQL BETWEEN operator?
We use the BETWEEN operator in SQL when we want to select values within a given range.
In how many ways can we define the range?
We can define ranges in as either numeric, date, text.
Can we find ranges within a date and time using BETWEEN the?
Yes, we can find ranges within mentioned date & time using BETWEEN statement.
How to get data between two timestamps in SQL?
The difference between the start and end timestamps is calculated by using the following query: SELECT TIMESTAMPDIFF(SECOND, start_timestamp, end_timestamp) FROM events WHERE event_name = ‘Meeting’;
|