# How to generate a series of dates in SQL

Source: https://sqlstreak.com/sql/how-to/generate-date-series · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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:

```sql
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:

```sql
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 (both engines):

| 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 |

(8 of 14 rows)

### A recursive CTE works in both

PostgreSQL runs the recursive version too; only the date step differs.

PostgreSQL:

```sql
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:

```sql
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 (both engines):

| day |
| --- |
| 2026-03-15 |
| 2026-03-16 |
| 2026-03-17 |
| 2026-03-18 |
| 2026-03-19 |
| 2026-03-20 |
| 2026-03-21 |

### generate_series isn't in this SQLite

```sql
SELECT * FROM generate_series(1, 3);
```

PostgreSQL:

| generate_series |
| --- |
| 1 |
| 2 |
| 3 |

SQLite: error: no such table: generate_series

## How it works

- `generate_series(start, end, INTERVAL '1 day')`: PostgreSQL: one row per day from start to end.
- `WITH RECURSIVE days(day) AS (… UNION ALL …)`: SQLite: start with the first day, then keep adding a day while it's before the end.
- `LEFT JOIN orders … COUNT(o.id)`: Every day stays; COUNT of a joined column is 0 when nothing matched.

## 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.
