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
inside range outside range
Stage 1 / 8
BETWEEN 50,000 AND 80,000 — both bounds are INCLUSIVE.
--example
query.sql
1SELECTname,salary
2FROMemployees
3WHEREsalaryBETWEEN50000AND80000;
Result
name
salary
Bob
52000
Elena
58000
Farid
73000
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
1SELECTname,salaryFROMemployees
2WHEREsalaryBETWEEN50000AND80000;
Result
name
salary
Bob
52000
Elena
58000
Farid
73000
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
1SELECTname,ageFROMemployees
2WHEREageBETWEEN26AND35
3ORDERBYage;
Result
name
age
Elena
26
Farid
33
Bob
35
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
1SELECTnameFROMemployees
2WHEREhire_date>='2024-01-01'
3ANDhire_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
1SELECTname,salaryFROMemployees
2WHEREsalaryNOTBETWEEN50000AND80000;
Result
name
salary
Alice
85000
Cynthia
91000
David
47000
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.
Practice SQL interview questions
Every SQL lesson maps to the matching interview topic, with answers written for current SQL engines.
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 →