---
title: "How to generate a sequence of dates in Redshift"
description: "How to use the generate_series function to generate a sequence of dates"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/generate-sequence-of-dates-in-redshift
---

In Redshift, when we need a sequence of dates between two given days, we can create it using the generate_series function and use it as a table in a FROM or JOIN clause.

It is useful when we need to display a table of dates and values, but we don't have a value for each of those days. Generating the series ensures that we have no missing dates because the data source does not contain anything on that day.

Here is how we can write a subquery that generates a series of dates between the current date and the day 30 days ago:

```sql
WITH dates AS (
    SELECT  generate_series AS N
    FROM generate_series(
        (NOW()::DATE - INTERVAL 30 days),
        ((NOW() - interval '1 day':: DATE), '1 day')
    )
)
```

