đ The BETWEEN operator in SQL is used to filter records whose values fall within a specified range. It can be used with numeric values, text, and dates. The range is inclusive, meaning both the starting and ending values are included in the result.
đ What is the BETWEEN Operator?
The BETWEEN operator simplifies range-based filtering. Instead of writing multiple comparison operators such as >= and <=, you can use a single, more readable expression.
Information
đ Basic Syntax
General Syntax
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | AdmissionDate |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | 2024-01-15 |
| 102 | Bob | Mathematics | 85 | 2024-02-10 |
| 103 | Charlie | Physics | 78 | 2024-03-20 |
| 104 | David | Computer Science | 95 | 2024-04-05 |
| 105 | Eva | Mathematics | 88 | 2024-05-12 |
đĸ BETWEEN with Numeric Values
Retrieve students whose marks are between 80 and 90.
Numeric BETWEEN Example
SELECT *
FROM Students
WHERE Marks BETWEEN 80 AND 90;This query returns students whose marks are greater than or equal to 80 and less than or equal to 90.
đ BETWEEN with Dates
The BETWEEN operator is commonly used to filter records within a date range.
Date Range Example
SELECT *
FROM Students
WHERE AdmissionDate
BETWEEN '2024-02-01' AND '2024-04-30';This query returns students admitted between February 1, 2024, and April 30, 2024, including both dates.
đ¤ BETWEEN with Text
BETWEEN can also compare text values. The results depend on the database's collation and sorting rules.
Text Range Example
SELECT *
FROM Students
WHERE Name BETWEEN 'A' AND 'D';Important
đĢ Using NOT BETWEEN
The NOT BETWEEN operator returns rows whose values fall outside the specified range.
NOT BETWEEN Example
SELECT *
FROM Students
WHERE Marks NOT BETWEEN 80 AND 90;This query returns students whose marks are less than 80 or greater than 90.
đ BETWEEN with AND
Combine BETWEEN with additional conditions using the AND operator.
BETWEEN with AND
SELECT *
FROM Students
WHERE Marks BETWEEN 80 AND 95
AND Department = 'Computer Science';đ BETWEEN with OR
You can also combine BETWEEN with the OR operator.
BETWEEN with OR
SELECT *
FROM Students
WHERE Marks BETWEEN 90 AND 100
OR Department = 'Physics';đ BETWEEN with ORDER BY
After filtering records with BETWEEN, you can sort the results using ORDER BY.
BETWEEN with ORDER BY
SELECT Name,
Marks
FROM Students
WHERE Marks BETWEEN 80 AND 95
ORDER BY Marks DESC;âī¸ BETWEEN vs Comparison Operators
| Using BETWEEN | Equivalent Comparison |
|---|---|
| Marks BETWEEN 80 AND 90 | Marks >= 80 AND Marks <= 90 |
| Date BETWEEN '2024-01-01' AND '2024-12-31' | Date >= '2024-01-01' AND Date <= '2024-12-31' |
đ Common Uses of BETWEEN
| Data Type | Example |
|---|---|
| Numbers | Price BETWEEN 100 AND 500 |
| Dates | OrderDate BETWEEN '2025-01-01' AND '2025-12-31' |
| Text | Name BETWEEN 'A' AND 'M' |
đŧ Real-World Example
A university administrator wants to generate a report showing students who scored between 85 and 95, sorted from the highest marks to the lowest.
Real-World Query
SELECT StudentID,
Name,
Department,
Marks
FROM Students
WHERE Marks BETWEEN 85 AND 95
ORDER BY Marks DESC;This query retrieves only students whose marks fall within the specified range and displays them in descending order.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ BETWEEN filters values within an inclusive range.
- đ It works with numbers, dates, and text values.
- đ NOT BETWEEN returns values outside the specified range.
- đ BETWEEN is equivalent to using >= and <=.
- đ It can be combined with AND, OR, and ORDER BY.
- đ Date and text comparisons depend on the stored values and database collation rules.