Course contentsShow
← Databases & SQL
Lesson 87 of 1,9532. Querying and Data RetrievalFree lesson

Inclusive Range Filtering

Define boundaries for numeric and temporal data using inclusive interval filtering.

In this lesson, you will learn how to capture a specific set of records by defining both a start and an end point that includes the boundary values themselves.

Why Use Inclusive Ranges?

When querying data, we often need to look for items within a specific window—like orders placed between two dates or products within a specific price range. An inclusive range means that if you specify a range from 10 to 20, both 10 and 20 are included in your final result set.

In SQL, the most common way to achieve this is by using the BETWEEN operator. This operator acts as a shorthand for checking if a value is greater than or equal to the start, and less than or equal to the end of your range.

Putting It Into Practice

Imagine you have a table called products and you want to find all items priced between 50and50 and 100. Using BETWEEN, your query would look like this:

SELECT product_name, price
FROM products
WHERE price BETWEEN 50 AND 100;

This query is mathematically equivalent to writing: WHERE price >= 50 AND price <= 100;

Beyond Numbers: Temporal Data

Inclusive range filtering is just as powerful when working with dates. Because dates act like a timeline, they follow the same logic. If you wanted to find every record created during the month of January 2023, you would define your boundaries to include the very first moment of the month and the very last:

SELECT event_name, event_date
FROM logs
WHERE event_date BETWEEN '2023-01-01' AND '2023-01-31';

Using BETWEEN makes your code much cleaner and easier to read than writing out two separate comparison conditions. It clearly communicates to anyone reading your code that you are looking for a complete, closed span of data. Just remember that BETWEEN is always inclusive, so the values you provide for your start and end points will always be included in the output if they exist in your dataset.

Key Takeaway: The BETWEEN operator is the standard tool for inclusive range filtering, ensuring your result set captures everything from your starting boundary to your ending boundary, inclusive of both.