Learn SQL Lesson
Filtering by Date Ranges
Dates are easier to work with when you treat them as dates, not strings. SQL lets you compare them directly.
To find February orders, you can write:
SELECT order_id, order_date
FROM orders
WHERE order_date >= DATE '2024-02-01'
AND order_date < DATE '2024-03-01'
This pattern is often safer than `BETWEEN` for month ranges, because the upper bound stays exclusive.
The difference matters most with timestamp values. If an order happened at `2024-02-29 18:30:00`, an inclusive upper bound like `BETWEEN DATE '2024-02-01' AND DATE '2024-02-29'` can miss it depending on how the database casts the date.
You can use the same idea for weeks, quarters, or any other date window.
Practice challenge
Return the February 2024 orders with order_id and order_date, ordered by order_date.