@HumairaM to SQLEnglish • 3 days ago

How do you write an SQL query to select records between two specific dates?

message-square
0
1
How do you write an SQL query to select records between two specific dates?
@HumairaM to SQLEnglish • 3 days ago

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:00 through 2026-11-30 23:59:59.999....
  • Excludes 2026-12-01 00:00:00 cleanly without guessing fractional seconds or milliseconds.
  • Avoids wrapping created_at in a function on the left side of WHERE, 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 DATETIME columns, '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_day is 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

  1. Forgetting time components on DATETIME columns: WHERE created_at BETWEEN '2026-11-01' AND '2026-11-30' stops at 2026-11-30 00:00:00, dropping rows from later in that day.
  2. Using ambiguous date formats like 04/05/2026: Always use unambiguous ISO format YYYY-MM-DD (or YYYY-MM-DDTHH:MM:SS) so regional settings (MM/DD vs. DD/MM) do not swap month and day.
  3. Swapped start and end dates: If start_date > end_date, BETWEEN returns zero rows without raising an error.