Restaurant Growth - Problem
Restaurant Growth Analysis

You are the owner of a popular restaurant and want to analyze your business growth for potential expansion. Your restaurant has been busy enough to guarantee at least one customer every day.

Given a Customer table with transaction data, you need to calculate the 7-day moving average of daily revenue. For each day, compute the average amount paid by customers over a 7-day window (current day + 6 previous days).

Table Schema:
Column NameType
customer_idint
namevarchar
visited_ondate
amountint

Requirements:
  • Primary key: (customer_id, visited_on)
  • Calculate moving average for 7-day windows
  • Round average_amount to 2 decimal places
  • Return results ordered by visited_on ascending
  • Only include dates that have a complete 7-day window

Input & Output

example_1.sql โ€” Basic Restaurant Data
$ Input: Customer table: | customer_id | name | visited_on | amount | |-------------|--------------|--------------|--------| | 1 | Jhon | 2019-01-01 | 100 | | 2 | Daniel | 2019-01-02 | 110 | | 3 | Jade | 2019-01-03 | 120 | | 4 | Khaled | 2019-01-04 | 130 | | 5 | Winston | 2019-01-05 | 110 | | 6 | Elvis | 2019-01-06 | 140 | | 7 | Anna | 2019-01-07 | 150 | | 8 | Maria | 2019-01-08 | 80 | | 9 | Jaze | 2019-01-09 | 110 |
โ€บ Output: | visited_on | amount | average_amount | |--------------|--------|----------------| | 2019-01-07 | 860 | 122.86 | | 2019-01-08 | 840 | 120.00 | | 2019-01-09 | 840 | 120.00 |
๐Ÿ’ก Note: Day 7 is the first day with a complete 7-day window (days 1-7). The amount is 100+110+120+130+110+140+150=860, and average is 860/7=122.86. For day 8, we exclude day 1 and include day 8: 110+120+130+110+140+150+80=840, average=120.00.
example_2.sql โ€” Multiple Customers Same Day
$ Input: Customer table: | customer_id | name | visited_on | amount | |-------------|---------|--------------|--------| | 1 | Alice | 2019-01-01 | 50 | | 2 | Bob | 2019-01-01 | 75 | | 3 | Carol | 2019-01-02 | 100 | | 4 | David | 2019-01-03 | 80 | | 5 | Eve | 2019-01-04 | 90 | | 6 | Frank | 2019-01-05 | 60 | | 7 | Grace | 2019-01-06 | 70 | | 8 | Henry | 2019-01-07 | 85 |
โ€บ Output: | visited_on | amount | average_amount | |--------------|--------|----------------| | 2019-01-07 | 510 | 72.86 |
๐Ÿ’ก Note: Multiple customers on the same day are aggregated. Day 1 total: 50+75=125. The 7-day window sum is 125+100+80+90+60+70+85=510, giving an average of 72.86.
example_3.sql โ€” Minimum Data Set
$ Input: Customer table: | customer_id | name | visited_on | amount | |-------------|-------|--------------|--------| | 1 | John | 2019-01-01 | 100 | | 2 | Jane | 2019-01-02 | 200 | | 3 | Bob | 2019-01-03 | 300 | | 4 | Alice | 2019-01-04 | 400 | | 5 | Carol | 2019-01-05 | 500 | | 6 | Dave | 2019-01-06 | 600 | | 7 | Eve | 2019-01-07 | 700 |
โ€บ Output: | visited_on | amount | average_amount | |--------------|--------|----------------| | 2019-01-07 | 2800 | 400.00 |
๐Ÿ’ก Note: This shows the minimum case where we have exactly 7 days of data. Only day 7 appears in results with sum=100+200+300+400+500+600+700=2800 and average=400.00.

Visualization

Tap to expand
Restaurant Growth: 7-Day Moving AverageRestaurantDaily RevenueDay 1
$100Day 2
$110
Day 3
$120
Day 4
$130
Day 5
$110
Day 6
$140
Day 7
$150
Day 8
$80
7-Day Window: Days 1-7Total: $860 | Average: $122.86Window Function MagicSUM() OVER (ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)Calculates all windows efficiently in one pass
Understanding the Visualization
1
Daily Aggregation
First, sum up all customer payments for each day to get daily totals
2
Sliding Window
For each day, look back 6 days plus current day to create 7-day windows
3
Moving Average
Calculate sum and average for each window, revealing growth trends
Key Takeaway
๐ŸŽฏ Key Insight: Window functions eliminate the need for expensive subqueries by calculating sliding windows in a single, optimized database operation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n log n) for sorting by date, then O(n) for window function calculation

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for daily aggregation and result set

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค customer_id โ‰ค 1000
  • 1 โ‰ค amount โ‰ค 1000
  • At least one customer visits the restaurant every day
  • visited_on dates are within a reasonable range
  • Primary key constraint: (customer_id, visited_on) is unique
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 22
42.5K Views
High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen