SQL BETWEEN Operator
beginnerfiltering
2 min read
What is the BETWEEN Operator?
BETWEEN filters rows where a column value falls within an inclusive range. It is shorthand for >= low AND <= high.
Syntax
SELECT * FROM table_name
WHERE column BETWEEN low AND high;When to Use
- Filtering dates within a range
- Selecting products in a price range
- Finding ages between two values
Key Points
- Inclusive —
BETWEEN 10 AND 20includes both 10 and 20. - NOT BETWEEN — Excludes the range:
WHERE age NOT BETWEEN 18 AND 25. - Date Ranges — Works well with dates:
WHERE created_at BETWEEN '2024-01-01' AND '2024-12-31'. - Order Matters — The lower bound must come first.
BETWEEN 20 AND 10returns no rows. - Timestamps — Be careful with timestamps:
BETWEEN '2024-01-01' AND '2024-01-31'may miss records on Jan 31 after midnight. Use< '2024-02-01'for precision.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL BETWEEN Operator Challenge
Write a query that find students between 18 and 22 years old.
Expected result
Students aged 18, 19, 20, 21, or 22.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is the BETWEEN Operator?
Initializing database...Each run starts from fresh sample data.
More Examples
Filter by date range
Find orders placed in January 2024.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Is BETWEEN inclusive or exclusive?
BETWEEN is inclusive on both ends. WHERE age BETWEEN 18 AND 25 includes ages 18 and 25.
Can I use BETWEEN with strings?
Yes. BETWEEN works with strings based on alphabetical order. WHERE name BETWEEN 'A' AND 'M' returns names starting with A through M.