← Back to SQL, page 2

sql-0022 · Medium mid

Is BETWEEN inclusive? Why can BETWEEN miss rows when the column stores a date and time?

BETWEEN a AND b is inclusive at both ends and means >= a AND <= b. With timestamps, BETWEEN '2026-03-01' AND '2026-03-02' excludes everything after midnight at the start of 2 March, so most of that day is missed. A half-open range, >= start AND < day after end, is the reliable pattern.

Full answer

x BETWEEN 10 AND 20 is shorthand for x >= 10 AND x <= 20, so both ends are included. With whole numbers or plain dates that is exactly what people expect.

Timestamps change the picture. Here are canteen payments with a date and time:

sql
CREATE TABLE payments (id INTEGER PRIMARY KEY, paid_at TEXT, amount INTEGER);
INSERT INTO payments (paid_at, amount) VALUES
  ('2026-03-01 09:10', 40), ('2026-03-02 00:00', 25),
  ('2026-03-02 13:45', 60), ('2026-03-03 08:00', 30);
SELECT id, paid_at, amount
FROM payments
WHERE paid_at BETWEEN '2026-03-01' AND '2026-03-02'
ORDER BY paid_at;
idpaid_atamount
12026-03-01 09:1040

The intent was "all payments on 1 and 2 March", but both 2 March payments are missing. As text, '2026-03-02 00:00' and '2026-03-02 13:45' start with '2026-03-02' and then keep going, so both are greater than '2026-03-02' and fail the <= half. Real timestamp types in PostgreSQL behave a little differently but have the same flaw: the date '2026-03-02' becomes midnight, so the midnight payment is included and everything later that day is not:

sql
SELECT id, paid_at, amount
FROM payments
WHERE paid_at BETWEEN '2026-03-01' AND '2026-03-02'
ORDER BY paid_at;
idpaid_atamount
12026-03-01 09:10:0040
22026-03-02 00:00:0025

A half-open range avoids the problem. Include the start, exclude the day after the end:

sql
SELECT id, paid_at, amount
FROM payments
WHERE paid_at >= '2026-03-01' AND paid_at < '2026-03-03'
ORDER BY paid_at;
idpaid_atamount
12026-03-01 09:1040
22026-03-02 00:0025
32026-03-02 13:4560

This form has two more advantages. Back-to-back ranges never overlap or leave gaps (March is >= '2026-03-01' AND < '2026-04-01'), and it works whatever the precision of the time part. Writing <= '2026-03-02 23:59:59' looks like a fix, but it breaks as soon as times carry fractions of a second.

NOT BETWEEN is inclusive in reverse: it means < a OR > b. And if a is greater than b, BETWEEN a AND b matches nothing, because the shorthand is not symmetric.

sql
CREATE TABLE payments (id INTEGER PRIMARY KEY, paid_at TEXT, amount INTEGER);
INSERT INTO payments (paid_at, amount) VALUES
  ('2026-03-01 09:10', 40), ('2026-03-02 00:00', 25),
  ('2026-03-02 13:45', 60), ('2026-03-03 08:00', 30);
SELECT id, paid_at, amount
FROM payments
WHERE paid_at BETWEEN '2026-03-01' AND '2026-03-02'
ORDER BY paid_at;

SELECT id, paid_at, amount
FROM payments
WHERE paid_at >= '2026-03-01' AND paid_at < '2026-03-03'
ORDER BY paid_at;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • BETWEEN includes both ends
  • With timestamps, the end date means midnight, so the last day is mostly excluded
  • Use >= start AND < next_day for date-time ranges
  • BETWEEN 20 AND 10 matches nothing

Common mistakes

  • Using BETWEEN with an end date on a timestamp column
  • Patching the range with 23:59:59 and missing fractional seconds

Follow-ups

  • How would you select all orders in the current calendar month?
  • Does BETWEEN work with text values?

Tips

  • Give the half-open form as the answer you would ship