Learn SQL
-- tables.datetime_functions

Date/Time Functions & Ranges in SQL

Half-open ranges, EXTRACT, DATE_TRUNC, INTERVAL

--concept

Compare typed literals (DATE '2024-01-01'), range half-open for timestamp days (>= start AND < next) — BETWEEN misses end-day times. EXTRACT(YEAR FROM x) and DATE_TRUNC('month', x) bucket; INTERVAL arithmetic offsets (created_at + INTERVAL '7 days'). Never compare dates as strings; never wrap indexed columns in functions (unsargable). Store TIMESTAMPTZ, display AT TIME ZONE.

--how it works
1

Range half-open

>= start AND < next keeps end-day times; BETWEEN drops them.

2

Truncate to bucket

DATE_TRUNC(month, x) for rollups; EXTRACT for parts.

3

Offset with INTERVAL

NOW() - INTERVAL 7 days — rolling windows without app math.

4

Zone at the edge

TIMESTAMPTZ UTC inside; AT TIME ZONE on display. Never VARCHAR dates.

--visualization
>= 2024-01-01 AND < 2024-02-01 — 2 rows, index seeks
order_idcreated_at
order_1012024-01-15
order_1022024-01-31
Stage 1 / 1
Bare column stays seekable; zones convert on display.
--example
query.sql
1SELECT * FROM orders WHERE created_at >= DATE '2024-01-01' AND created_at < DATE '2024-02-01';
Result
order_idcreated_at
order_1012024-01-15
order_1022024-01-31

Half-open keeps Jan-31 afternoon rows that BETWEEN '2024-01-01' AND '2024-01-31' would drop at midnight. Raw column stays bare — index seeks instead of scanning.

--variants

January done right

Keeps Jan-31 afternoon rows.

query.sql
1SELECT * FROM orders WHERE created_at >= DATE '2024-01-01' AND created_at < DATE '2024-02-01';
Result
order_idcreated_at
order_1012024-01-15
order_1022024-01-31
1Lower bound inclusive, upper exclusive.
2BETWEEN .. AND 2024-01-31 misses afternoon times.
3Raw column bare — index seeks.

Monthly buckets + last-7-days

Truncate groups; interval filters.

query.sql
1SELECT DATE_TRUNC('month', created_at) AS m, COUNT(*) FROM orders GROUP BY m;
2SELECT * FROM orders WHERE created_at >= NOW() - INTERVAL '7 days';
Result
monthorders
2024-01-012
2024-02-011
1Truncate collapses to month starts for GROUP BY.
2Interval arithmetic stays inside SQL — no app date math.
3DST-safe because TIMESTAMPTZ + interval, not naive + 24h.
--key facts
Half-open>= start AND < next — timestamp-safe
BETWEEN trapdrops end-day partial data
Bare columnfunctions on column kill index seeks
--use cases
Reporting ranges + cohort buckets
Rolling windows: last-7/30-day screens
--under the hood
DATE_TRUNC on indexed column can use expression indexes; raw-column ranges use plain B-trees.
VARCHAR dates mis-sort and mis-compare — typed literals only.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.