← All SQL interview questions
Zero-filled daily sales grid for every store
MediumJoins — Cross JoinWalmartTarget
A regional manager wants a complete store-by-day sales grid for the week of 20 to 24 October 2026. Days on which a store sold nothing must still appear, with sales of 0.
Using stores, report_days and daily_sales, return store_name, report_date and sales_amount (total sales for that store and day, 0 if none). Order by store_name, then report_date.
Sample data
Table: stores
4 row(s)
| store_idINT | store_nameTEXT |
|---|---|
| 1 | Whitefield |
| 2 | Indiranagar |
| 3 | Koramangala |
| 4 | Jayanagar |
Table: report_days
5 row(s)
| report_dateDATE |
|---|
| 2026-10-20 |
| 2026-10-21 |
| 2026-10-22 |
| 2026-10-23 |
| 2026-10-24 |
Table: daily_sales
12 row(s) · showing first 8
| sale_idINT | store_idINT | sale_dateDATE | sales_amountNUMERIC(10,2) |
|---|---|---|---|
| 1 | 1 | 2026-10-20 | 42000.00 |
| 2 | 1 | 2026-10-21 | 38500.00 |
| 3 | 1 | 2026-10-23 | 51000.00 |
| 4 | 2 | 2026-10-20 | 29000.00 |
| 5 | 2 | 2026-10-20 | 6000.00 |
| 6 | 2 | 2026-10-22 | 33000.00 |
| 7 | 2 | 2026-10-24 | 47500.00 |
| 8 | 3 | 2026-10-21 | 22000.00 |
stores
- store_idINT
- store_nameTEXT
report_days
- report_dateDATE
daily_sales
- sale_idINT
- store_idINT
- sale_dateDATE
- sales_amountNUMERIC(10,2)
- Step 1: `CROSS JOIN` builds every combination of store and day: 4 stores x 5 days = 20 rows.
- Step 2: `LEFT JOIN` the real sales onto that grid so combinations with no sales are kept.
- Step 3: Indiranagar has two rows on 20 October, so `SUM` per store and day is needed (35000).
- Step 4: `COALESCE(SUM(...), 0)` turns the NULL produced for empty days into 0.
SELECT s.store_name,
d.report_date,
COALESCE(SUM(ds.sales_amount), 0) AS sales_amount
FROM stores s
CROSS JOIN report_days d
LEFT JOIN daily_sales ds
ON ds.store_id = s.store_id
AND ds.sale_date = d.report_date
GROUP BY s.store_id, s.store_name, d.report_date
ORDER BY s.store_name, d.report_date;More Joins — Cross Join questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program