SQL
5 min read
BEGINNER
Date range predicates
Quick AnswerFilter a date or timestamp interval using an inclusive start and exclusive end. This avoids guessing the last possible time of a day or month.
Core idea
Filter a date or timestamp interval using an inclusive start and exclusive end. This avoids guessing the last possible time of a day or month.
Minimal example
sql
SELECT id FROM filter_events WHERE occurred_at >= '2025-01-01 00:00:00' AND occurred_at < '2025-02-01 00:00:00' ORDER BY id;Expected SQLite rows:
text
23How it works
The supplemental event fixture uses fixed-width ISO date/time strings (YYYY-MM-DD HH:MM:SS), all representing the same time basis. Lexical comparisons work for this fixture; real PostgreSQL applications should use typed dates or timestamps and appropriate time-zone boundaries. The February 1 midnight row must be excluded from January.
Practical example
sql
SELECT id FROM filter_events WHERE occurred_at >= '2025-01-31 00:00:00' AND occurred_at < '2025-02-01 00:00:00' ORDER BY id;Expected SQLite rows:
text
3Common mistake and practice
Avoid BETWEEN with an end timestamp at midnight of the last day: later events that day may be missed. Write a February filter using February 1 and March 1 as boundaries; expect ID 4.