What is BETWEEN?
SQLLevel: FoundationAggregations Basics
What is BETWEEN?
Hints
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
BETWEEN is inclusive on both ends. BETWEEN 1 AND 10 is the same as >= 1 AND <= 10.
SELECT * FROM orders
WHERE total_amount BETWEEN 100 AND 500;
-- Equivalent to:
SELECT * FROM orders
WHERE total_amount >= 100 AND total_amount <= 500;
Watch out with dates: BETWEEN '2024-01-01' AND '2024-01-31' on a timestamp column might miss rows at 2024-01-31 12:00:00 depending on the database. Safer:
WHERE created_at >= '2024-01-01' AND created_at < '2024-02-01'
Source and licence: SQL Interview Questions — Arda Kocadoru · MIT