Is SQL Between Inclusive

Galaxy Glossary

How does the BETWEEN operator work in SQL, and is it inclusive?

The SQL BETWEEN operator is used to select values within a range. Crucially, it's inclusive, meaning the start and end values are part of the selection.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The `BETWEEN` operator in SQL is a convenient way to filter data based on a range of values. It's particularly useful when you need to select rows where a column's value falls within a specific minimum and maximum. A key aspect of `BETWEEN` is that it's inclusive; both the starting and ending values are considered part of the range. This differs from some other comparison operators, where the endpoints might be excluded. For example, if you want to find all orders placed between 2023-01-01 and 2023-01-31, you'd use `BETWEEN`. This ensures that orders placed on January 1st and January 31st are included in the results. Understanding the inclusive nature of `BETWEEN` is essential for accurate data retrieval. It's a straightforward way to filter data, avoiding the need for multiple comparison operators. Using `BETWEEN` improves readability and maintainability of your SQL queries. It's a fundamental operator for range-based filtering in SQL.

Why Is SQL Between Inclusive is important

The `BETWEEN` operator simplifies range-based filtering, making SQL queries more readable and maintainable. Its inclusive nature ensures that you retrieve all the data within the specified range, which is crucial for accurate reporting and analysis.

Example Usage


-- Sample table (Orders)
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    OrderDate DATE,
    Amount DECIMAL(10, 2)
);

INSERT INTO Orders (OrderID, OrderDate, Amount) VALUES
(1, '2023-01-15', 100.00),
(2, '2023-02-20', 200.00),
(3, '2023-01-01', 150.00),
(4, '2023-01-31', 50.00),
(5, '2023-03-10', 120.00);

-- Query to retrieve orders placed between January 1st and January 31st, inclusive
SELECT OrderID, OrderDate, Amount
FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-01-31';

Common Mistakes

Want to learn about other SQL terms?