SQLSQL
#44

Calculate Rolling 7-Day Moving Sums in SQL

HardAmazon
Interview Question #44
Asked at Amazon

Given 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_onamount
2026-01-01100
2026-01-02110
2026-01-03120
2026-01-04130
2026-01-05110
2026-01-06140
2026-01-07150
2026-01-0880
Expected Output Structure2 rows
visited_onamountaverage_amount
2026-01-07860122.86
2026-01-08840120
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us