Hone

Lessons · SQL · BETWEEN is inclusive

A range, inclusive

BETWEEN a AND b means a <= x AND x <= b, both ends included.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Date ranges, price bands, age brackets.

How to think about it

Does the column hold times as well as dates? For dates stored with times, BETWEEN '2024-04-01' AND '2024-04-30' misses April 30 after midnight. Use >= start AND < the day after the end.

Worked example

WHERE released BETWEEN '2024-04-01' AND '2024-04-30'
Fine for date-only columns.
WHERE released >= '2024-04-01' AND released < '2024-05-01'
Correct for timestamps too.

Your turn

Prices from 10 to 20 inclusive.

WHERE price  10 AND 20

The trap

Reversing the ends: BETWEEN 20 AND 10 matches nothing.

Practise BETWEEN is inclusive on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.