When working with databases, it’s often necessary to filter records based on a range of values. Whether you’re looking for data between certain dates, within a specific numeric range, or even between lexicographically defined text values, the SQL BETWEEN
operator is an essential tool.
The BETWEEN
operator simplifies your SQL queries by allowing you to easily filter results between two values. In this blog post, we’ll explore how to use the BETWEEN
operator effectively, with practical examples to help you understand its various uses.
The BETWEEN
operator in SQL is used to filter records that fall within a specified range. The range can be applied to numbers, dates, or text. The BETWEEN
operator is inclusive, meaning it includes both the starting and ending values of the range.
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
column_name
: The column you're applying the filter to.value1
: The starting value of the range.value2
: The ending value of the range.BETWEEN
are inclusive, meaning the values value1
and value2
are included in the result.value1
and value2
does not matter. SQL will automatically treat them as the lower and upper bounds, respectively, even if they are swapped.The BETWEEN
operator is commonly used with numeric values to filter rows within a specific range. This is useful when you're working with financial data, sales figures, or any type of numeric range.
Suppose you have a products
table with a price
column, and you want to find all products priced between $10 and $50.
SELECT product_name, price
FROM products
WHERE price BETWEEN 10 AND 50;
Result:
product_name | price |
---|---|
Widget A | 15.00 |
Widget B | 30.00 |
Widget C | 40.00 |
This query returns all products with prices between $10 and $50, including $10 and $50.
The BETWEEN
operator works with any numeric data type (e.g., INT
, DECIMAL
, FLOAT
), and it is inclusive, meaning both the minimum and maximum values will be part of the result.
The BETWEEN
operator can also be used to filter data within a date range. This is useful for filtering records based on a specific period, such as sales transactions within a certain time frame.
Suppose you have an orders
table with an order_date
column, and you want to find all orders placed between January 1, 2024, and December 31, 2024.
SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';
Result:
order_id | order_date |
---|---|
1 | 2024-01-15 |
2 | 2024-05-20 |
3 | 2024-08-10 |
This query retrieves all orders placed in the year 2024.
YYYY-MM-DD
in most SQL databases).The BETWEEN
operator is not limited to numbers and dates. It can also be used to filter results within a lexicographical range when working with text (strings). This can be helpful for filtering names, categories, or other string-based columns.
Suppose you have a customers
table with a last_name
column, and you want to find all customers whose last names fall between "Jones" and "Smith" alphabetically.
SELECT customer_id, first_name, last_name
FROM customers
WHERE last_name BETWEEN 'Jones' AND 'Smith';
Result:
customer_id | first_name | last_name |
---|---|---|
1 | John | Johnson |
2 | Jane | Johnson |
3 | Alice | Lee |
This query returns all customers whose last names come alphabetically between "Jones" and "Smith".
BETWEEN
operator for strings works lexicographically, which means it compares strings based on their alphabetical order.While the BETWEEN
operator is quite efficient for filtering data, there are a few things to keep in mind for performance:
BETWEEN
will be quite good, as the database can use the index to quickly narrow down the results.BETWEEN
range is too large (e.g., selecting a wide range of dates or numbers), it may affect query performance. Consider narrowing your range or optimizing your query.BETWEEN
operator does not work with NULL
values. If you need to check for NULL
values in your dataset, you’ll need to handle them separately (e.g., using IS NULL
).The NOT BETWEEN
operator is the opposite of BETWEEN
and can be used to filter data outside of a specific range. This is useful when you need to exclude a certain range of values.
SELECT column1, column2, ...
FROM table_name
WHERE column_name NOT BETWEEN value1 AND value2;
Let’s say you want to find products whose price is not between $10 and $50:
SELECT product_name, price
FROM products
WHERE price NOT BETWEEN 10 AND 50;
Result:
product_name | price |
---|---|
Widget X | 5.00 |
Widget Y | 100.00 |
This query will return products that are either priced below $10 or above $50.