SQL Dates
Dates become easier to query when they are stored consistently. Our sample uses ISO year-month-day text for plain dates. Timestamp systems also need a clear time zone policy and a distinction between an instant and a local calendar date.
Understand the idea
A half-open range includes the first boundary and excludes the next period's start. It works for both plain dates and consistently represented timestamps without inventing a final time such as 23:59:59. SQLite provides date functions rather than a dedicated date storage class. Other engines have native DATE and timestamp types with their own rules.
Where to try it
Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.
SQLite sample data · MySQL / MariaDB sample data · See the sample tables
A worked example
SELECT order_id, ordered_at
FROM orders
WHERE ordered_at >= '2026-01-01'
AND ordered_at < '2026-02-01'
ORDER BY ordered_at, order_id;
Read the result
Orders 101 and 102 are in January. February orders are outside the range. Applying the comparison directly to the stored column can also make an ordinary index more useful than wrapping that column in a function.
Try one change
Adapt the query for February. Explain how you would decide the boundaries for a user's local month when timestamps are stored in UTC.