BETWEEN (Range)
Filter values inside a range with BETWEEN … AND …, remembering that both ends are included.
"Players with 500 to 750 runs." "Innings played between 25 and 31 March." Range questions are everywhere, and BETWEEN writes them in a short, readable way.
Definition
BETWEEN tests whether a value lies within a range.
expr BETWEEN low AND highis TRUE whenexpr >= low AND expr <= high— both boundary values are included (it is an inclusive range).
Key words: inclusive — 500 and 750 themselves are kept. low first — the smaller value must come first, otherwise no row can match.
Syntax
sql
SELECT column_list
FROM table_name
WHERE column_name BETWEEN low_value AND high_value;
-- opposite: outside the range
WHERE column_name NOT BETWEEN low_value AND high_value;| Part | Meaning |
|---|---|
BETWEEN | start of the range test |
low_value | lower boundary (included) |
AND | separates the two boundaries — here it is part of BETWEEN, not the logical AND |
high_value | upper boundary (included) |
NOT BETWEEN | expr < low OR expr > high — boundaries excluded |
Example — runs from 500 to 750
sql
SELECT *
FROM players
WHERE runs BETWEEN 500 AND 750;| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 5 | David Warner | DC | Australia | 700 |
BETWEEN includes BOTH ends: Rohit (exactly 500) and Virat (exactly 750) are kept. This is identical to WHERE runs >= 500 AND runs <= 750.
Example — NOT BETWEEN
sql
SELECT *
FROM players
WHERE runs NOT BETWEEN 500 AND 750;| id | name | team | country | runs |
|---|---|---|---|---|
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 350 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Example — BETWEEN with dates
sql
SELECT player, match_date, runs
FROM innings
WHERE match_date BETWEEN '2025-03-25' AND '2025-03-31';| player | match_date | runs |
|---|---|---|
| Virat Kohli | 2025-03-29 | 59 |
| Rohit Sharma | 2025-03-30 | 76 |
| MS Dhoni | 2025-03-31 | NULL |
| Jos Buttler | 2025-03-25 | 73 |
Both 25 March and 31 March are included. (BETWEEN tested match_date, so Dhoni's NULL runs do not matter here.)
BETWEEN on text
Text compares alphabetically, which can surprise you:
sql
SELECT name FROM players WHERE name BETWEEN 'D' AND 'M';| name |
|---|
| Jos Buttler |
| David Warner |
| Jasprit Bumrah |
MS Dhoni is missing! 'MS Dhoni' is greater than 'M' (it is longer and starts with M), so it is outside the range. For "names starting D to M" use name >= 'D' AND name < 'N'.
Common mistakes
- ❌
BETWEEN 750 AND 500(high first) → returns 0 rows in SQL Server. ✅ alwaysBETWEEN low AND high. - ❌ Thinking BETWEEN excludes the ends. ✅ it is inclusive on both sides.
- ❌ Datetime trap: on a
DATETIMEcolumn,BETWEEN '2025-03-25' AND '2025-03-31'stops at 31 March 00:00:00, missing anything later that day. ✅ usematch_time >= '2025-03-25' AND match_time < '2025-04-01'. - ❌
WHERE runs BETWEEN 500, 750→ a comma is not allowed. ✅BETWEEN 500 AND 750.
Interview tip
Q: Is BETWEEN inclusive or exclusive in SQL Server?
A: Inclusive on both ends: x BETWEEN a AND b means x >= a AND x <= b. NOT BETWEEN is exclusive: x < a OR x > b.
Key points to remember
- BETWEEN low AND high keeps values inside the range, including both ends.
- x BETWEEN a AND b is exactly x >= a AND x <= b.
- Always write the smaller value first — BETWEEN 750 AND 500 returns no rows.
- NOT BETWEEN keeps values outside the range (boundaries excluded).
- BETWEEN works with numbers, dates and text (text uses alphabetical order).
- For DATETIME columns, prefer >= start AND < next_day to avoid missing times.
Try it yourself
Runs from 500 to 750 (inclusive)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NOT BETWEEN and reversed boundaries
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
BETWEEN with dates
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: innings from 50 to 80
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
