Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL 7-Day Rolling Average Sales

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL 7-Day Rolling Average Sales. Start with the requirements and the one table on the right.

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.

Problem

Digitas México needs a daily performance view for products promoted through its digital campaigns. Write a PostgreSQL query that calculates each product's seven-day rolling average of daily completed sales.

Requirements

  1. Aggregate sales by product_name and sale_date.
  2. Count only rows where sale_status = 'completed'; treat dates with no completed sales as zero.
  3. Calculate the average of the current day and up to the six preceding daily rows for each product.
  4. Return results ordered by product and date, with the rolling average rounded to two decimal places.

Schema

campaign_sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_nameVARCHAR(100)Product associated with the campaign
sale_dateDATEDate of the sale
sale_amountNUMERIC(10,2)Sale amount
sale_statusVARCHAR(20)Sale processing status
Tablescampaign_sales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results