To select records between two specific dates in SQL, use either a half-open range (>= and <)—which is strongly recommended when working with DATETIME or timestamp columns—or the BETWEEN operator for inclusive date/range boundaries. Always watch out for time-of-day cutoffs on timestamps because '2026-11-30' without a time defaults to 00:00:00, which excludes records created later on November 30.
1. Recommended for Timestamps / DATETIME: Half-Open Range (>= and <)
When a column stores dates and times (such as DATETIME, DATETIME2, or TIMESTAMP), using >= on the start date and < on the day after the end date captures every record on the end date while keeping queries index-friendly (SARGable).
SELECT
order_id,
customer_id,
created_at,
total_amount
FROM orders
WHERE created_at >= '2026-11-01T00:00:00'
AND created_at < '2026-12-01T00:00:00';
- Includes all timestamps from
2026-11-01 00:00:00through2026-11-30 23:59:59.999.... - Excludes
2026-12-01 00:00:00cleanly without guessing fractional seconds or milliseconds. - Avoids wrapping
created_atin a function on the left side ofWHERE, which helps indexes work efficiently.
2. Inclusive Boundaries with BETWEEN
BETWEEN start_val AND end_val is equivalent to column >= start_val AND column <= end_val. It works cleanly on DATE columns or explicitly bounded timestamps.
On a DATE Column (YYYY-MM-DD)
SELECT
employee_id,
leave_date,
leave_type
FROM employee_leaves
WHERE leave_date BETWEEN '2026-11-01' AND '2026-11-30';
Including Time Boundaries on a DATETIME Column
SELECT
order_id,
created_at,
total_amount
FROM orders
WHERE created_at BETWEEN '2026-11-01 00:00:00' AND '2026-11-30 23:59:59';
Note: On
DATETIMEcolumns,'2026-11-30 23:59:59'misses the final second of the day, whereas'2026-11-30 23:59:59.997'or'23:59:59.9999999'depends on precision—another reason>=and< next_dayis safer for timestamps.
3. Using Parameterized Dates or Date Functions
In applications or scheduled scripts, pass date parameters cleanly rather than concatenating strings, or use database date functions when filtering relative to the current date.
-- Example using SQL Server / PostgreSQL / MySQL style date literals
SELECT
invoice_id,
invoice_date,
amount_due
FROM invoices
WHERE invoice_date >= DATE '2026-11-15'
AND invoice_date < DATE '2026-12-01'
ORDER BY invoice_date ASC;
Common Pitfalls to Avoid
- Forgetting time components on
DATETIMEcolumns:WHERE created_at BETWEEN '2026-11-01' AND '2026-11-30'stops at2026-11-30 00:00:00, dropping rows from later in that day. - Using ambiguous date formats like
04/05/2026: Always use unambiguous ISO formatYYYY-MM-DD(orYYYY-MM-DDTHH:MM:SS) so regional settings (MM/DDvs.DD/MM) do not swap month and day. - Swapped start and end dates: If
start_date > end_date,BETWEENreturns zero rows without raising an error.