Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: 7-Day Rolling Orders Per Shopper

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

Your question is SQL: 7-Day Rolling Orders Per Shopper. 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

Instacart analyzes shopper workload across fulfillment zones such as North and Central. Write a PostgreSQL query that calculates the seven-day rolling average of completed orders for each shopper and fulfillment zone.

Requirements

  1. Join completed orders to the shopper and fulfillment zone dimensions.
  2. Aggregate completed orders by shopper, zone, and calendar date.
  3. Use a window function partitioned by shopper and zone, with a date-based seven-day frame covering the current date and the prior six days.
  4. Return one row for each shopper-zone-date that has at least one completed order, ordered by shopper, zone, and date.

Schema

shoppers
ColumnTypeDescription
shopper_idPKINTEGERUnique shopper identifier
shopper_nameVARCHAR(100)Shopper display name
home_zone_idINTEGERShopper's assigned home fulfillment zone
fulfillment_zones
ColumnTypeDescription
zone_idPKINTEGERUnique fulfillment zone identifier
zone_nameVARCHAR(100)Fulfillment zone name
orders
ColumnTypeDescription
order_idPKINTEGERUnique order identifier
shopper_idINTEGERAssigned shopper identifier
zone_idINTEGERFulfillment zone identifier
order_dateDATECalendar date of the order
statusVARCHAR(20)Order processing status
Tablesordersshoppersfulfillment_zones
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results