Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
7-Day Rolling Average Sales
00:00
5 left

7-Day Rolling Average Sales

MediumSQL · PostgreSQL

Problem

Walmart Labs wants category-level sales trends for Walmart's digital commerce reporting. Write a PostgreSQL query that calculates a seven-day rolling average of daily sales for each product category.

The rolling window must cover the current date and the six preceding calendar dates. Dates with no sales must count as zero, and categories with products but no sales during the period must still appear.

Requirements

  1. Aggregate sales to one row per category and calendar date.
  2. Generate dates from 2024-01-01 through 2024-01-07 and fill missing sales with zero.
  3. Use a window function partitioned by category to calculate the rolling average.
  4. Return results ordered by category and sales date.

Schema

products
ColumnTypeDescription
product_idPKINTEGERUnique product identifier
product_nameVARCHAR(100)Product display name
categoryVARCHAR(50)Product category
sales
ColumnTypeDescription
sale_idPKINTEGERUnique sale identifier
product_idINTEGERProduct sold
sale_dateDATEDate on which the sale occurred
sales_amountNUMERIC(12,2)Monetary value of the sale
Tablesproductssales
Interviewer

Your question is 7-Day Rolling Average Sales. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.