Menu

How to get a list of dates between two dates in SQL?

Written by Selva Prabhakaran | 3 min read

Problem

You need to retrieve a list of dates between two specified dates.

Input

First, let’s create a table and insert some sample data into it.

startDate endDate
2023-01-01 2023-01-05
2023-02-01 2023-02-03

For this example, the table contains two date ranges: Jan 1 to Jan 5 and Feb 1 to Feb 3 of 2023.

Try Hands-On: HERE

Source Tables: Gist

Desired Solution

dateValue
2023-01-01
2023-01-02
2023-01-03
2023-01-04
2023-01-05
2023-02-01
2023-02-02
2023-02-03

Solution 1:

Using Recursive Approach

sql
WITH RECURSIVE DateSeries AS (
    SELECT startDate AS dateValue
    FROM DateRange
    UNION ALL
    SELECT DATE_ADD(dateValue, INTERVAL 1 DAY)
    FROM DateSeries
    WHERE DATE_ADD(dateValue, INTERVAL 1 DAY) <= (SELECT endDate FROM DateRange WHERE DATE_ADD(dateValue, INTERVAL 1 DAY) BETWEEN startDate AND endDate)
)
SELECT DISTINCT dateValue
FROM DateSeries
ORDER BY dateValue;

Explanation:

  • The recursive CTE starts by selecting the startDate from the DateRange table.

  • In the subsequent recursive part of the CTE (UNION ALL), we use DATE_ADD to add one day to the current dateValue
    .-
    The WHERE clause inside the recursive part ensures that the date addition only proceeds as long as the new date is still between the startDate and endDate of any row in the DateRange table

  • .
    Finally, outside the CTE, we select the distinct dates to eliminate any duplicate date values and order the result by dat
    ble.

Solution 2:

An alternative method to retrieve a list of dates between two dates in MySQL involves using a helper table (or number series). This approach uses a table that simply has a sequence of numbers, which we can use to generate a range of dates.

First, we’ll create a helper table named NumberSeries:

sql
CREATE TABLE NumberSeries (n INT);

-- Let's insert a series of numbers. This will cover a range for up to 1000 days. You can increase it if needed.
DELIMITER ;
BEGIN
    DECLARE i INT DEFAULT 0;
    WHILE i < 1000 DO
        INSERT INTO NumberSeries (n) VALUES (i);
        SET i = i + 1;
    END WHILE;
END;
DELIMITER ;

Solution:

sql
SELECT DATE_ADD(dr.startDate, INTERVAL ns.n DAY) AS dateValue
FROM DateRange dr
JOIN NumberSeries ns
ON DATE_ADD(dr.startDate, INTERVAL ns.n DAY) BETWEEN dr.startDate AND dr.endDate
ORDER BY dateValue;

Explanation:

  • We use a JOIN between the DateRange table and the NumberSeries table.

  • The DATE_ADD function with the ns.n value from the NumberSeries table is used to add days to the startDate.
    The BETWEEN condition in the ON clause ensures that the generated date falls within the range of startDate and endDate for each row in the DateRange table.

  • Lastly, we order the results by the generated date (dateValue).

  • By using this solution, you can retrieve the list of dates between the start and end dates for each row in the DateRange table.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to access previous row value in SQL?
  2. What is the difference between CROSS Join and INNER Join?
  3. How to transpose columns to rows in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science