BETWEEN example
SELECT id, date, status, total
FROM orders
WHERE date BETWEEN '2025-06-01' AND '2025-06-10'
ORDER BY date, id;Check the result with small data
We use amounts just inside and just outside the bounds of the range.
WITH payments(id, amount) AS (
VALUES (1, 10), (2, 20), (3, 30), (4, 31)
)
SELECT id, amount FROM payments
WHERE amount BETWEEN 20 AND 30 ORDER BY id;| id | amount |
|---|---|
| 2 | 20 |
| 3 | 30 |
BETWEEN includes both ends: 20 and 30 appear, and 31 is left out. With dates that include times, think carefully about the upper bound.
Check your understanding: Would the payment of 10 appear if we changed the minimum to 10?
Show answer
Yes. With BETWEEN 10 AND 30, 10 becomes the lower limit and BETWEEN includes its ends: payments 1, 2 and 3 would appear. The one for 31 would still be left out.
How BETWEEN is used
Write the lower bound first and then the upper bound; do not assume the engine will swap them. If any of the values being compared is NULL, the condition is not true.
What changes from case to case
The example works because date stores whole days in ISO format. If the column includes a time, BETWEEN '2025-06-01' AND '2025-06-10' may treat the second bound as the start of 10 June and leave out later times on that day.
For periods of time, a half-open interval is usually more robust: include the start and exclude the start of the next period.
WHERE created_at >= '2025-06-01'
AND created_at < '2025-07-01'This covers the whole of June without having to invent a “last moment” of the month.
Common mix-ups with BETWEEN
Thinking the upper bound is excluded
BETWEEN 50 AND 100 accepts 50 and 100. To exclude them, write comparisons with > and <.
Comparing dates stored as text that does not sort correctly
Formats such as «10/06/2025» do not keep chronological order when compared as text. Use the engine's date types or a consistent, documented format.