SQL to select rows between two dates
Return rows whose date falls within a date range using BETWEEN or a pair of comparisons.
SELECT *
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-01-31';BETWEEN includes both '2024-01-01' and '2024-01-31'. Fine for a pure DATE column.
SELECT *
FROM orders
WHERE created_at >= '2024-01-01'
AND created_at < '2024-02-01';A timestamp like '2024-01-31 14:00' is later than '2024-01-31 00:00:00', so BETWEEN ... AND '2024-01-31' would drop it. >= start AND < next-day captures the whole last day.
How to select rows between two dates
Filtering a date range is most readable with BETWEEN, but BETWEEN is inclusive on both ends — which trips people up on timestamp columns. Use a half-open range for timestamps.
How it works
- BETWEEN a AND b is shorthand for col >= a AND col <= b — both ends are included.
- On a timestamp column the upper bound '2024-01-31' means midnight, so rows later that day are excluded — use < the next day instead.
- Comparing the bare column (not a function of it) lets the database use an index on the date column.
Tip
For date ranges on a timestamp column, prefer the half-open pattern (>= start AND < end) over BETWEEN to avoid silently dropping the last day.
Related SQL queries
- SQL to get the day of the week
- SQL to get the days between two dates
- SQL to get rows from the last 30 days
- SQL to find duplicate rows
- SQL to delete duplicate rows (keep one)
- SQL to find the second highest value
Frequently asked questions
How do I select rows between two dates in SQL?
Filtering a date range is most readable with BETWEEN, but BETWEEN is inclusive on both ends — which trips people up on timestamp columns. Use a half-open range for timestamps. BETWEEN a AND b is shorthand for col >= a AND col <= b — both ends are included. On a timestamp column the upper bound '2024-01-31' means midnight, so rows later that day are excluded — use < the next day instead. Comparing the bare column (not a function of it) lets the database use an index on the date column.
Does this work in PostgreSQL, MySQL, SQL Server, and SQLite?
Yes — this page lists the query for each dialect, since the syntax can differ between database engines.
Can I generate this query for my own tables?
Yes. Describe what you want in plain English with Forgly's free AI SQL Generator and it writes the query for your dialect.