Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 7-Day Sales Trends

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

Your question is Rolling 7-Day Sales Trends. Start with the requirements and the three tables 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

How would you use SQL window functions to calculate rolling 7-day sales trends by product and region for W.W. Grainger?

Write a PostgreSQL query using the provided sales, products, and regions data. Include dates with sales activity, calculate daily sales, and calculate the total for the current date plus the preceding six calendar days.

Output

  1. One row per product, region, and sales date with activity.
  2. Columns: product_name, region_name, sales_date, daily_sales, and rolling_7_day_sales.
  3. Sort by product_name, region_name, and sales_date ascending.

Schema

sales
ColumnTypeDescription
sale_idPKINTUnique sales transaction identifier
product_idINTProduct associated with the sale
region_idINTSales region associated with the transaction
sales_dateDATEDate of the sale
sales_amountNUMERIC(12,2)Sales amount for the transaction
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Grainger product name
regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(100)Sales region name
Tablessalesproductsregions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results