January revenue with BETWEEN
This should total all orders placed in January. Finance says the number is too low. Why? (created_at is a TIMESTAMP.)
SELECT SUM(total_cents) AS january_revenueFROM ordersWHERE created_at BETWEEN '2025-01-01' AND '2025-01-31';Show answer
line 3 · Off-by-one
'2025-01-31' means midnight at the start of Jan 31, and BETWEEN is inclusive of exactly that instant. Every order placed later on Jan 31 is excluded.
WHERE created_at >= '2025-01-01' AND created_at < '2025-02-01';Interview follow-up: Half-open ranges (>= start, < next start) work for every precision and avoid end-of-month arithmetic.