BETWEEN
Filter values within a range — inclusive on both ends
Beginner 8 minBETWEENrange
Range Filtering
BETWEEN checks if a value falls within a range. It's inclusive on both ends — BETWEEN 10 AND 50 includes both 10 and 50 themselves. Here's what to know:
Equivalent to AND —
WHERE price BETWEEN 20 AND 100 behaves identically to WHERE price >= 20 AND price <= 100. BETWEEN is simply more readable, especially for date ranges and numeric bands in business queries.Works with dates — For dates stored in ISO format (YYYY-MM-DD), BETWEEN handles date ranges correctly because alphabetical order matches chronological order. Example:
BETWEEN '2024-01-01' AND '2024-12-31' covers the full year.NOT BETWEEN — Excludes the range and returns values below the lower bound or above the upper bound. Useful for finding outliers or records that fall outside a normal expected range.
Timestamp pitfall — For timestamp columns,
BETWEEN '2024-01-01' AND '2024-12-31' is interpreted as BETWEEN '2024-01-01 00:00:00' AND '2024-12-31 00:00:00', which misses every time on Dec 31 after midnight. Use a half-open interval instead: ts >= '2024-01-01' AND ts < '2025-01-01'.Knowledge Canvas
How BETWEEN Works
Filter within a range (inclusive)
- BETWEEN includes both endpoints — it's [low, high]
- Equivalent to:
col >= low AND col <= high - Works with numbers, dates, and strings
- NOT BETWEEN excludes the range
BETWEEN Gotchas
Inclusivity traps
- BETWEEN is inclusive on BOTH ends — off-by-one risk with dates
- Date BETWEEN '2024-01-01' AND '2024-01-31' misses Jan 31 timestamps
- Reversed order: BETWEEN 100 AND 10 → always empty
- NULLs: NULL BETWEEN 1 AND 10 → NULL (excluded)
Date Range Patterns
Safe date filtering
- Use >= and < for dates:
date >= '2024-01-01' AND date < '2024-02-01' - BETWEEN works cleanly for date-only columns
- For timestamps, exclusive end avoids midnight issues
- Combine with date functions for dynamic ranges
BETWEEN Rules
Keep these in mind
- Always low value first: BETWEEN small AND large
- Both bounds are included in results
- Works with any comparable type
- NOT BETWEEN = outside the range (exclusive)
Syntax
Syntax Template
1WHERE column BETWEEN low
2 AND high;
3
4-- Exclude a range
5-- WHERE column NOT BETWEEN low
6-- AND high;
BETWEEN a AND bInclusive range: a <= column <= bNOT BETWEENOutside the range: column < a OR column > b Sample Data
products
10 rows
| name | price |
|---|---|
| Wireless Mouse | 24.99 |
| Office Desk | 189 |
| LED Desk Lamp | 45.5 |
| USB-C Cable | 12.99 |
| Bluetooth Speaker | 79 |
| Office Chair | 95 |
| Coffee Maker | 89.99 |
| Notebook Set | 8.5 |
| Wireless Headphones | 159 |
| Standing Desk | 425 |
orders
12 rows
| id | order_date | total |
|---|---|---|
| 5001 | 2024-01-15 | 59.98 |
| 5002 | 2024-02-08 | 449 |
| 5003 | 2024-02-20 | 12.5 |
| 5004 | 2024-03-05 | 89.99 |
| 5005 | 2024-03-15 | 245 |
| 5006 | 2024-03-22 | 159 |
| 5007 | 2024-04-02 | 45.5 |
| 5008 | 2024-04-12 | 79 |
| 5009 | 2024-04-22 | 425 |
| 5010 | 2024-05-05 | 18.99 |
| 5011 | 2024-05-18 | 134 |
| 5012 | 2024-06-08 | 65.5 |
Worked Example
Find products priced between 20 and 100.
SQL
1SELECT
2 name,
3 price
4FROM products
5WHERE price BETWEEN 20
6 AND 100
7ORDER BY
8 price;
BETWEEN 20 AND 100 includes both endpoints. Products at exactly 20 or 100 would be included. USB Hub (19.99) is just below the range, so it is excluded.
Output
5 rows
| name | price |
|---|---|
| Wireless Mouse | 24.99 |
| LED Desk Lamp | 45.5 |
| Bluetooth Speaker | 79 |
| Coffee Maker | 89.99 |
| Office Chair | 95 |
Key Concepts
1Both boundaries are included: BETWEEN 10 AND 50 includes 10 and 50
2Date ranges work in ISO format: BETWEEN '2024-01-01' AND '2024-12-31'
3BETWEEN is shorthand for >= AND <= — identical behavior
4NOT BETWEEN returns values outside the range
Pro Tip
Range queries are everywhere in business analysis — date ranges, price bands, age groups, score brackets.
When to Use
Orders within a date range, products in a price band, employees within an age bracket, scores within a grade range.
Find all orders from the orders table placed between '2024-03-01' and '2024-04-30'. Show id, order_date, and total.
Your Query