SQL how-to
How to generate a series of dates in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
PostgreSQL: generate_series(DATE '2026-03-15', DATE '2026-03-28', INTERVAL '1 day'). SQLite: a recursive CTE that adds a day until the end date. LEFT JOIN your data to it and COUNT a column, and empty days show 0.
The task: Produce one row per day in a range, so days with no data still show up.
Orders per day, including days with none
PostgreSQL
SELECT d::date AS day, COUNT(o.id) AS orders
FROM generate_series(DATE '2026-03-15', DATE '2026-03-28', INTERVAL '1 day') AS d
LEFT JOIN orders o ON o.ordered_on = d::date
GROUP BY d
ORDER BY d;SQLite
WITH RECURSIVE days(day) AS (
SELECT '2026-03-15'
UNION ALL
SELECT date(day, '+1 day') FROM days WHERE day < '2026-03-28'
)
SELECT day, COUNT(o.id) AS orders
FROM days
LEFT JOIN orders o ON o.ordered_on = day
GROUP BY day
ORDER BY day;Result (PostgreSQL and SQLite, identical): first 12 of 14 rows
| day | orders |
|---|---|
| 2026-03-15 | 1 |
| 2026-03-16 | 0 |
| 2026-03-17 | 0 |
| 2026-03-18 | 1 |
| 2026-03-19 | 0 |
| 2026-03-20 | 1 |
| 2026-03-21 | 0 |
| 2026-03-22 | 2 |
| 2026-03-23 | 0 |
| 2026-03-24 | 0 |
| 2026-03-25 | 0 |
| 2026-03-26 | 0 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
generate_series(start, end, INTERVAL '1 day')PostgreSQL: one row per day from start to end.
- 2
WITH RECURSIVE days(day) AS (… UNION ALL …)SQLite: start with the first day, then keep adding a day while it's before the end.
- 3
LEFT JOIN orders … COUNT(o.id)Every day stays; COUNT of a joined column is 0 when nothing matched.
More examples
A recursive CTE works in both
PostgreSQL runs the recursive version too; only the date step differs.
PostgreSQL
WITH RECURSIVE days(day) AS (
SELECT DATE '2026-03-15'
UNION ALL
SELECT (day + 1)::date FROM days WHERE day < DATE '2026-03-21'
)
SELECT day FROM days;SQLite
WITH RECURSIVE days(day) AS (
SELECT '2026-03-15'
UNION ALL
SELECT date(day, '+1 day') FROM days WHERE day < '2026-03-21'
)
SELECT day FROM days;Result (PostgreSQL and SQLite, identical): 7 rows
| day |
|---|
| 2026-03-15 |
| 2026-03-16 |
| 2026-03-17 |
| 2026-03-18 |
| 2026-03-19 |
| 2026-03-20 |
| 2026-03-21 |
Try it: PostgreSQL playground · SQLite playground
generate_series isn't in this SQLite
SELECT * FROM generate_series(1, 3);PostgreSQL: 3 rows
| generate_series |
|---|
| 1 |
| 2 |
| 3 |
SQLiteno such table: generate_series
Try it: PostgreSQL playground · SQLite playground
Variations
Months instead of days
INTERVAL '1 month' in PostgreSQL, date(day, '+1 month') in SQLite.
Common mistakes
COUNT(*)after the LEFT JOIN: an empty day still has one row, so it counts 1.Joining a timestamp to a date: compare the same types, or cast the timestamp to date.
A recursive CTE without a stopping condition: it runs until it hits a limit.
PostgreSQL and SQLite
PostgreSQL has generate_series; standard SQLite builds, including this one, don't (some ship it as an extension), so SQLite uses WITH RECURSIVE, which PostgreSQL also understands.
Official documentation
Go deeper
- Guide: SQL recursive CTEs (WITH RECURSIVE)
- Lesson: WITH RECURSIVE (Pro)
Make it stick in five minutes a day
“WITH RECURSIVE” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.