Learn SQL
-- filtering.between

BETWEEN — Range Filter in SQL

Inclusive lower AND upper bounds

--concept

BETWEEN low AND high tests a value against an inclusive range — both endpoints count. WHERE salary BETWEEN 50000 AND 80000 keeps 50000, 73000 and 80000 alike. It works on numbers, dates and even text (alphabetical ranges). Logically it is exactly x >= low AND x <= high, just shorter to read.

--how it works
1

Range with two bounds

WHERE salary BETWEEN 50000 AND 80000 — the AND belongs to the operator, not to Boolean logic.

2

Both ends are inclusive

50000 and 80000 exactly both match — the interview gotcha.

3

It is shorthand

Logically identical to salary >= 50000 AND salary <= 80000; same plan, fewer words.

4

Dates are the danger zone

BETWEEN '2024-01-01' AND '2024-12-31' loses Dec 31 timestamps after midnight — prefer >= start AND < next_year.

5

Negate with NOT BETWEEN

Keeps only outliers — values below low or above high.

--visualization
50,00080,000AAlice · 85,000BBob · 52,000CCynthia · 91,000DDavid · 47,000EElena · 58,000FFarid · 73,000
inside range outside range
Stage 1 / 8
BETWEEN 50,000 AND 80,000 — both bounds are INCLUSIVE.
--example
query.sql
1SELECT name, salary
2FROM employees
3WHERE salary BETWEEN 50000 AND 80000;
Result
namesalary
Bob52000
Elena58000
Farid73000

Six salaries enter the range band; three sit inside it including neither endpoint here — but 50000 or 80000 exactly would have stayed too. Drag the range handles in the visualization.

--variants

Salary band

Inclusive on both ends.

query.sql
1SELECT name, salary FROM employees
2WHERE salary BETWEEN 50000 AND 80000;
Result
namesalary
Bob52000
Elena58000
Farid73000
1Three salaries sit inside [50000, 80000].
2Had someone earned exactly 50000 or 80000, they would match too.
3NOT BETWEEN would return the other three.

Age band

Same operator, integer column.

query.sql
1SELECT name, age FROM employees
2WHERE age BETWEEN 26 AND 35
3ORDER BY age;
Result
nameage
Elena26
Farid33
Bob35
1Elena (26) and Bob (35) prove both endpoints are inclusive.
2Alice 29 and Cynthia 41 — 29 is inside, 41 is out.
3ORDER BY sorts the survivors young-to-old.

Date range done right

Half-open intervals avoid the timestamp trap.

query.sql
1SELECT name FROM employees
2WHERE hire_date >= '2024-01-01'
3 AND hire_date < '2025-01-01';
1Our sample table has no hire_date column — this query is the pattern to remember.
2< '2025-01-01' includes every instant of 2024, down to 23:59:59.999.
3BETWEEN '2024-01-01' AND '2024-12-31' would drop Dec 31 daytime rows.

NOT BETWEEN

Keep only the outliers.

query.sql
1SELECT name, salary FROM employees
2WHERE salary NOT BETWEEN 50000 AND 80000;
Result
namesalary
Alice85000
Cynthia91000
David47000
1The complement of the band: below 50000 or above 80000.
2David (47000) sits under the floor; Alice and Cynthia over the ceiling.
3Same three rows the salary-band case excluded — two queries, one partition.
--key facts
InclusiveBETWEEN 1 AND 3 counts 1, 2 and 3
Date traptimestamp BETWEEN day-start AND midnight loses the last day
NegationNOT BETWEEN keeps only the outliers
--use cases
Price/age/score sliders in filter UIs — a range control is literally BETWEEN
Date and time windows for reports (with the half-open pattern)
Banding rows: CASE salary WHEN BETWEEN … tiered buckets
Sanity checks: WHERE age NOT BETWEEN 0 AND 120 finds bad data
--under the hood
A BETWEEN on an indexed column uses the index for both bounds — one range seek, not two.
For datetimes, the half-open >= start AND < end pattern is index-friendly AND correctness-friendly.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.