SQL BETWEEN
BETWEEN a AND b is a range filter — inclusive on both ends. It works on numbers, dates, and strings.
Examples
SQL
-- Numeric range SELECT * FROM products WHERE price BETWEEN 10 AND 50; -- Date range (be careful — see below) SELECT * FROM orders WHERE created_at BETWEEN '2026-01-01' AND '2026-01-31'; -- String range — alphabetical SELECT * FROM customers WHERE name BETWEEN 'A' AND 'D';
Inclusive vs exclusive — the date trap
BETWEEN '2026-01-01' AND '2026-01-31' looks like "all of January". But if created_at is a DATETIME, an order at 2026-01-31 23:59:59 is included, while an order at 2026-02-01 00:00:00 isn't — fine. The problem comes when someone changes the column to a different precision. A safer idiom for date-only ranges:
SQL
WHERE created_at >= '2026-01-01' AND created_at < '2026-02-01';
NOT BETWEEN
SQL
SELECT * FROM products WHERE price NOT BETWEEN 10 AND 50;
Tip: Always put the smaller value first.
BETWEEN 50 AND 10 returns nothing — there are no values that are both ≥ 50 and ≤ 10.Example
Exercise
Match prices between 10 and 50 inclusive.
WHERE price
10 AND 50
Seven letters; inclusive-range keyword.
Discussion
Loading…