- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL BETWEEN Operator
The BETWEEN operator in SQL is a logical operator used to filter values within a specific, continuous range. Instead of writing multiple comparison operators like >= and <=, you can use BETWEEN to write cleaner, more readable queries. It is versatile and works with numeric data, text strings, and date values.
BETWEEN as a shorthand for (column >= value1 AND column <= value2). It makes your SQL code much easier for other team members to read and maintain.
Basic Syntax of BETWEEN Operator
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
column_name: The specific column you want to filter.value1andvalue2: The start and end points of your range. These values are inclusive.
BETWEEN 500 AND 100).
Key Features of BETWEEN
1. Inclusive Range
One of the most important things to remember is that the BETWEEN operator includes both the start and end values you provide in the results.
SELECT product_name, price
FROM products
WHERE price BETWEEN 100 AND 500;
- This query retrieves products where the price is exactly 100, exactly 500, or anything in the middle.
> and < operators instead.
2. Works with Dates
In real-world applications, BETWEEN is most commonly used to generate reports for specific timeframes, such as monthly sales or quarterly logs.
SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-06-30';
- This retrieves every order placed from the very first second of January 1, 2024, through the end of June 30, 2024.
DATETIME columns. If your column contains timestamps (like 2024-06-30 14:30:00), a search BETWEEN '2024-01-01' AND '2024-06-30' might exclude items from the final day because the database defaults the end time to 00:00:00.
3. Works with Text Values
When used with strings, BETWEEN filters based on alphabetical (dictionary) order. This is useful for finding ranges of names or categories.
SELECT name
FROM customers
WHERE name BETWEEN 'A' AND 'M';
- This retrieves names starting with A, B, C, and so on, up to (and including) names that are exactly 'M'.
BETWEEN 'A' AND 'M' because 'Miller' comes after 'M' in the dictionary. To include all 'M' names, use BETWEEN 'A' AND 'Mz' or 'N'.
4. NOT BETWEEN
If you need to find "outliers" or data points that fall outside of a specific range, you can use NOT BETWEEN.
SELECT product_name, price
FROM products
WHERE price NOT BETWEEN 100 AND 500;
- This retrieves products that are either cheaper than 100 or more expensive than 500.
Example Queries
1. Filter Products by Price Range
Useful for e-commerce "price filter" features where a user selects a budget range.
SELECT product_name, price
FROM products
WHERE price BETWEEN 50 AND 200;
2. Filter Orders by Date Range
Standard for financial reporting and auditing.
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date BETWEEN '2024-03-01' AND '2024-03-31';
3. Exclude Salaries Outside a Range
Often used by HR departments to identify entry-level roles or executive-level roles by excluding the middle-management pay scale.
SELECT name, salary
FROM employees
WHERE salary NOT BETWEEN 3000 AND 7000;
4. Filter Names Alphabetically
SELECT name
FROM employees
WHERE name BETWEEN 'D' AND 'K';
Combining BETWEEN with Other Conditions
In a production environment, you rarely filter by just one column. BETWEEN can be seamlessly combined with AND, OR, and IN clauses to build complex business logic.
SELECT name, salary, department
FROM employees
WHERE salary BETWEEN 4000 AND 8000
AND department = 'IT'
AND status = 'Active';
- This query targets active IT staff who fall within a specific "mid-tier" salary bracket.
BETWEEN with OR conditions, use parentheses to ensure the database engine evaluates your logic in the correct order.
Summary
The BETWEEN operator is an essential tool for any SQL developer's toolkit. It provides a clean and readable way to filter inclusive ranges across numbers, dates, and strings. By understanding its inclusive nature and how it handles different data types, you can write more efficient queries and avoid common pitfalls like missing timestamp data or incorrect alphabetical boundaries.