SQL
All Top 50 SQL Interview Questions#44
Calculate Rolling 7-Day Moving Sums in SQL
HardAmazonInterview Question #44
Asked at AmazonGiven a `customer_orders` table (customer_id, visited_on, amount), calculate the 7-day rolling sum and 7-day moving average of amount for each day starting from the 7th day onward, rounded to 2 decimal places.
Input Table: daily_sales
8 rows preview| visited_on | amount |
|---|---|
| 2026-01-01 | 100 |
| 2026-01-02 | 110 |
| 2026-01-03 | 120 |
| 2026-01-04 | 130 |
| 2026-01-05 | 110 |
| 2026-01-06 | 140 |
| 2026-01-07 | 150 |
| 2026-01-08 | 80 |
Expected Output Structure2 rows
| visited_on | amount | average_amount |
|---|---|---|
| 2026-01-07 | 860 | 122.86 |
| 2026-01-08 | 840 | 120 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.