Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.