Quick revision
The short answer
We generate the 31 days of March 2026 with generate_series, CROSS JOIN them with stores to get every store-day pair, and LEFT JOIN sales that we have already summed per store and day in a subquery, using COALESCE to turn the missing sums into 0. Aggregating first keeps one row per store and day before the join, so nothing can multiply. In MySQL, which has no generate_series, we produce the dates with a recursive CTE or a calendar table.
What to remember
In this answer, we built the March report by generating the days, crossing them with every store to get a complete grid, and left joining sales that we summed per store and day first, with COALESCE turning missing sums into zero. We saw why aggregating before the join prevents fan-out, how a WHERE condition on the left-joined table or COUNT(*) brings back the wrong answer, and how MySQL does the same with a recursive CTE or a calendar table.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We generate the 31 days of March 2026 with generate_series, CROSS JOIN them with stores to get every store-day pair, and LEFT JOIN sales that we have already summed per store and day in a subquery, using COALESCE to turn the missing sums into 0. Aggregating first keeps one row per store and day before the join, so nothing can multiply. In MySQL, which has no generate_series, we produce the dates with a recursive CTE or a calendar table.
Why this question is asked
Dashboards and finance reports almost always want a row for every period, and the zero days matter: a chart that skips Sundays looks like steady sales, and a sum of averages over missing days is wrong. Interviewers like this exercise because it combines three ideas, generating rows that do not exist in any table, building a grid with a cross join, and joining aggregated data without changing it, and because there are two classic mistakes that make the zeros vanish. In this answer, we will build the query in steps, see each mistake and why it happens, and finish with the MySQL version and an index.
1A Quick Recap: Why the Zeros Are Missing
A GROUP BY over the sales table can only return groups that exist in the data. If store 7 sold nothing on 9 March, there is no row for store 7 on that day, so no amount of grouping can make one appear. To show it, the query must start from a set that contains every store and every day, and attach the sales to it.
That set is a cartesian product, the combination of every row of one input with every row of another, here every store with every day. PostgreSQL's generate_series function produces the days. Called with two dates and an interval, it returns one value per step, as timestamps, so we cast each one back to a date with ::date.
2The Grid of Stores and Days
First we build the grid on its own and check its size. With 40 stores and 31 days we expect exactly 1,240 rows.
Both end dates are included, so March has 31 values. Notice that the grid comes from stores, not from sales: a store that sold nothing all month still gets 31 rows, which is exactly what a report of all stores wants. If the report should only include open stores, we filter stores here, not later.
3Aggregate Sales First, Then LEFT JOIN
Now we sum the sales per store and day in a derived table, a subquery in FROM, and left join it to the grid. The left join keeps every grid row and fills the sales columns with NULL where there is no match, and COALESCE replaces that NULL with 0.
- Generate 31 daysgenerate_series, cast to date
- CROSS JOIN stores40 x 31 = 1,240 pairs
- Sum sales per store and dayone row per pair that had sales
- LEFT JOIN the sums to the gridunmatched pairs get NULL
- COALESCE(total, 0)1,240 rows, zeros included
5 steps in order. Play them, or step through them one at a time.
The reason for aggregating first is fan-out, the multiplication of rows when one side of a join has several matches. The subquery returns at most one row per store and day, so the join can never duplicate a grid row. If we later add another per-day source, such as refunds, and join raw rows of both tables to the grid, each sale would pair with each refund of the same day and both sums would be inflated. Summing each source on its own first, then joining the sums, keeps every number right. It also shrinks the join: a few thousand summed rows instead of every individual sale. The date filter uses a half-open range, from the first of March up to but not including the first of April, which stays correct even if sold_on is later changed to a timestamp.
4Two Mistakes That Make the Zeros Disappear
The first mistake is filtering the right-hand table in WHERE after a left join. The WHERE clause runs after the join, and for the zero days the sales columns are NULL, so a condition on them is not true and the row is removed. The left join silently becomes an inner join.
The fix is to move such conditions into the ON clause of the left join, or into the derived table as we did. The second mistake is counting with COUNT(*) after a left join: it counts the grid row itself, so a day with no sales shows 1 sale instead of 0. COUNT(sa.id) counts only matched rows, because COUNT of a column skips NULLs, and gives the correct 0.
5The MySQL Version and an Index
MySQL has no generate_series, so we generate the days with a recursive CTE, available since MySQL 8.0, or join to a permanent calendar table that many teams keep with one row per date for years ahead.
A calendar table is often the better long-term choice because it can also carry business facts, such as whether a date is a holiday or which fiscal week it belongs to. For speed in either database, an index on sales(sold_on, store_id) lets the inner query read only March, and including amount in the index, for example with INCLUDE (amount) in PostgreSQL, lets it sum without visiting the table.
6Follow-up questions
Why not fill the missing days in Java instead?
We can: query the sums, then loop over the dates and stores and put 0 where the map has no entry. It is a fine choice when the report is built in the service anyway. Doing it in SQL keeps the result complete for every consumer, such as a BI tool, an export or another query that joins to it, and it avoids each consumer reimplementing the gap filling, sometimes differently.
What changes if sold_on is a timestamp with time zone?
Then 'the day' depends on a time zone, and we must say which. We would aggregate on the local date, for example (sold_at AT TIME ZONE 'Europe/Berlin')::date, and generate the days as plain dates. Filtering should still use a half-open range on the raw column, computed in the same zone, so an index on sold_at can be used. Mixing server time and store time is a very common source of sales appearing on the wrong day.
Is generate_series inside a CROSS JOIN slow?
No. It produces 31 rows in memory and costs almost nothing. The cost of the query is the sales aggregation, which depends on how many sales rows March has and whether an index lets the database read only that month.
7Conclusion
In this answer, we built the March report by generating the days, crossing them with every store to get a complete grid, and left joining sales that we summed per store and day first, with COALESCE turning missing sums into zero. We saw why aggregating before the join prevents fan-out, how a WHERE condition on the left-joined table or COUNT(*) brings back the wrong answer, and how MySQL does the same with a recursive CTE or a calendar table.
Open with
The zero days do not exist in the sales table, so I start from a grid that contains every store and every day. In PostgreSQL I generate the days with generate_series from the first to the thirty-first of March 2026 with a one-day interval, cast each value to a date, and CROSS JOIN that with stores.
Then, if they want more
With 40 stores that is 1,240 rows. Then I LEFT JOIN the sales, but I aggregate them first in a subquery: sum of amount grouped by store_id and sold_on, filtered to March with a half-open range.
Joining the summed rows means each grid row matches at most one row, so there is no fan-out, and if I later add refunds or other sources, each is summed separately and nothing gets double counted. In the outer select I use COALESCE of the total to zero.
Two things I watch for: a filter on the sales table in WHERE after the left join removes the zero days, so conditions go in ON or in the subquery, and if I count sales I use COUNT of a sales column, not COUNT star, which would count one for an empty day.
In MySQL there is no generate_series, so I would use a recursive CTE that adds one day at a time, or better, a calendar table. An index on sales by sold_on and store_id keeps the aggregation to just March.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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.
- Official documentation
- PostgreSQL documentation, The SQL Language
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 SQL that produces daily sales per store for every day of March 2026, showing 0 for...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/c91b155ae1/