Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Write a recursive CTE that generates every date between two dates in MySQL 8.4, and use it to show daily signups including days with zero signups.

We generate the dates with WITH RECURSIVE d AS (SELECT DATE('2025-06-01') AS day UNION ALL SELECT day + INTERVAL 1 DAY FROM d WHERE day < '2025-06-30'), then LEFT JOIN users on a half-open range for each day and GROUP BY d.day. COUNT(u.id) counts only matched users, so empty days show 0, while COUNT(*) would show 1. MySQL stops recursion after cte_max_recursion_depth iterations (1000 by default), and for real reporting a permanent calendar table, or generate_series in PostgreSQL, is often the better tool.

Why this question is asked

A signup chart with missing days is misleading: if nobody signed up on a Sunday, a plain GROUP BY simply leaves Sunday out, and the chart draws a straight line from Saturday to Monday as if nothing unusual happened. Interviewers ask this question because filling those gaps needs three ideas working together: a generated series of dates, an outer join, and the right kind of COUNT. In this answer, we will see why GROUP BY alone loses empty days, build the date series with a recursive CTE in MySQL 8.4, join it to users correctly, look at the COUNT trap and the recursion limit, and finish with the alternatives a team would use in production.

1Why GROUP BY Alone Loses Empty Days

GROUP BY can only make groups out of rows that exist. If we group users by the date part of created_at, every day with at least one signup becomes a group, and every day without signups simply does not appear, because there is no row to put in it.

SQL
SELECT DATE(created_at) AS day, COUNT(*) AS signups
FROM users
WHERE created_at >= '2025-06-01' AND created_at < '2025-07-01'
GROUP BY DATE(created_at)
ORDER BY day;
-- 2025-06-01 | 14
-- 2025-06-02 | 9
-- 2025-06-04 | 11 <- 2025-06-03 is missing, not 0

So we need a set of rows that contains every day, whether or not anything happened on it, and then we attach the signups to it. That set of days is called a date series, or a calendar.

2Generating the Dates With a Recursive CTE

MySQL has supported recursive CTEs since version 8.0, so on 8.4 we can generate the dates ourselves. The anchor produces the first day, and the recursive part adds one day to the previous row until it reaches the last day.

SQL
WITH RECURSIVE d AS (
SELECT DATE('2025-06-01') AS day -- anchor: one row
UNION ALL
SELECT day + INTERVAL 1 DAY FROM d -- next day
WHERE day < '2025-06-30' -- stop after the 30th
)
SELECT day FROM d;
-- 2025-06-01
-- 2025-06-02
-- ...
-- 2025-06-30 (30 rows)

Notice that the stopping condition is checked on the previous row. When the previous row is 29 June, the condition is true and the recursive part produces 30 June. When the previous row is 30 June, the condition is false, no row is produced, and the recursion ends. The type of the column comes from the anchor, which is why we wrap the literal in DATE(): it makes day a DATE rather than a string.

3Joining Signups to Every Day

Now we LEFT JOIN users to the series. A LEFT JOIN keeps every row from the left side, the days, even when no user matches; the user columns are then NULL.

SQL
WITH RECURSIVE d AS (
SELECT DATE('2025-06-01') AS day
UNION ALL
SELECT day + INTERVAL 1 DAY FROM d WHERE day < '2025-06-30'
)
SELECT d.day, COUNT(u.id) AS signups
FROM d
LEFT JOIN users u
ON u.created_at >= d.day
AND u.created_at < d.day + INTERVAL 1 DAY
GROUP BY d.day
ORDER BY d.day;
-- 2025-06-01 | 14
-- 2025-06-02 | 9
-- 2025-06-03 | 0
-- 2025-06-04 | 11
-- ... 30 rows
What the LEFT JOIN produces for three days
DayJoined user rowsCOUNT(u.id)COUNT(*)
2025-06-029 rows with real ids99
2025-06-031 row with u.id = NULL01
2025-06-0411 rows with real ids1111
An empty day still produces one joined row, with NULLs on the users side.

The join condition is a half-open range: from midnight at the start of the day, inclusive, to midnight at the start of the next day, exclusive. It is better than ON DATE(u.created_at) = d.day for two reasons. It is correct for timestamps at any precision, including 23:59:59.999. And it leaves created_at untouched, so an index on users(created_at) can be used for each day's range; wrapping the column in DATE() would hide it from the index.

The filter on the users side belongs in the ON clause, not in a WHERE clause. A WHERE condition on u columns would throw away the rows where u is NULL and turn our LEFT JOIN back into an inner join, losing the empty days again.

4The COUNT Trap: COUNT(u.id) Versus COUNT(*)

This is the line that most often goes wrong. For a day with no signups, the LEFT JOIN still produces one row: the day, with NULL in every users column. COUNT(*) counts rows, so it counts that row and reports 1 signup for an empty day. COUNT(u.id) counts only non-NULL values of u.id, so it reports 0.

So we always count a column from the outer-joined table, and preferably its primary key, which is never NULL in a real row. Then a NULL can only mean 'no match'.

5The Recursion Limit in MySQL

MySQL protects itself against runaway recursion with the system variable cte_max_recursion_depth, which is 1000 by default. One iteration produces one day, so a range of up to about 1000 days works, but three years of dates fails with an error that tells us to raise the variable (the exact text varies; check your version).

SQL
SET SESSION cte_max_recursion_depth = 5000; -- for this connection only
-- or for one statement, with an optimizer hint:
WITH RECURSIVE d AS (...)
SELECT /*+ SET_VAR(cte_max_recursion_depth = 5000) */ d.day, COUNT(u.id) ...

We raise it for the session or the statement rather than globally, so other queries keep the protection. A recursion limit is a safety net, not a design tool: if a report regularly needs ten years of days, that is a sign we want a permanent calendar table.

6Better Options for Production Reports

In PostgreSQL the recursive CTE is unnecessary, because generate_series('2025-06-01'::date, '2025-06-30'::date, interval '1 day') produces the series directly. The rest of the query, the LEFT JOIN and COUNT(u.id), stays the same.

For real reporting on either engine, a permanent calendar table is better still. It holds one row per day for, say, 2000 to 2050, about 18,000 rows, with extra columns that reports keep needing: day of week, ISO week, fiscal period, public holiday flags per country. Reports then join to it like any table, and rules such as 'business days only' become a simple WHERE is_business_day.

One last caution is time zones. created_at is usually stored in UTC, but a user in Sydney who signs up at 08:00 local time on 3 June is recorded on 2 June in UTC. If the chart is for an Australian business, we convert the day boundaries to the business time zone before joining, otherwise every day is shifted.

7Follow-up questions

Why not generate the dates in Java and send them to the database?

We could, but it moves the logic out of one statement into two places and needs a way to pass a list of dates, such as a VALUES list or a temporary table. Generating the series in SQL keeps the report in one query that is easy to test and reuse. Filling gaps in Java after a plain GROUP BY is also reasonable for a small chart, as long as everybody agrees where the gap-filling lives.

How would you show weekly signups instead of daily?

Change the step to INTERVAL 7 DAY, start on a Monday, and join on the range [week_start, week_start + 7 days). With a calendar table, we can instead group by its iso_week column, which also handles years that start in the middle of a week.

Can a recursive CTE in MySQL use LIMIT to stop?

Since MySQL 8.0.19, a LIMIT in the recursive SELECT caps the total number of rows the CTE produces, which is another way to stop it. A clear WHERE condition on the date is easier to read for this case, so I would use LIMIT only as an extra guard.

8Conclusion

In this answer, we filled the missing days in a signup report. A recursive CTE in MySQL 8.4 generates one row per day, a LEFT JOIN on a half-open time range attaches signups without hiding the index, and COUNT(u.id) returns 0 for empty days where COUNT(*) would return 1. MySQL limits recursion with cte_max_recursion_depth, 1000 by default, which we raise per session or statement for long ranges, and in PostgreSQL generate_series or, for serious reporting, a permanent calendar table is the simpler and richer choice.

What to say in the interviewAbout 90 seconds aloud

Open with

A plain GROUP BY on the date can't return days with no signups, because there are no rows to group. So I generate the days first. In MySQL 8.4 that's a recursive CTE: WITH RECURSIVE d AS (SELECT DATE('2025-06-01') AS day UNION ALL SELECT day plus INTERVAL 1 DAY FROM d WHERE day is before June 30th).

Then, if they want more

Then I select d.day and COUNT(u.id) from d LEFT JOIN users on created_at greater than or equal to the day and less than the next day, grouped and ordered by day. The half-open range is correct for any timestamp precision and lets an index on created_at be used, which DATE(created_at) would not.

The important detail is COUNT(u.id) rather than COUNT star: an empty day still produces one joined row with NULLs, so COUNT star would show 1 while COUNT of the id shows 0. I also keep the user condition in the ON clause so the outer join is not turned into an inner join.

MySQL limits recursion with cte_max_recursion_depth, default 1000, so for longer ranges I raise it for the session. In PostgreSQL I would just use generate_series, and for real reporting I'd prefer a calendar table with weekdays, holidays and fiscal periods, and I'd be careful to cut days in the business time zone.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.4, MySQL 8.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a recursive CTE that generates every date between two dates in MySQL 8.4, and use...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/a0181828ce/