Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Users and Basket Size

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

Your question is Rolling Users and Basket Size. Start with the requirements and the two 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's retail partner teams want a daily view of customer activity and basket economics. Write a PostgreSQL query that calculates rolling 7-day active users and average basket size for each retail partner.

Use completed orders from January 1 through January 8, 2024. A rolling 7-day window includes the report date and the six preceding calendar days. Return one row for each partner and date with at least one completed order during the reporting period.

Requirements

  1. Count distinct users with a completed order for the partner within the rolling seven-day calendar window.
  2. Calculate the average basket_amount for completed orders in the same window, rounded to two decimal places. Ignore NULL basket amounts using PostgreSQL aggregate behavior.
  3. Exclude cancelled orders and orders outside the reporting period when determining report dates or contributing metrics.
  4. Sort the output by partner name and report date.

Schema

partners
ColumnTypeDescription
partner_idPKINTEGERUnique retail partner identifier
partner_nameVARCHAR(100)Retail partner name
orders
ColumnTypeDescription
order_idPKINTEGERUnique order identifier
partner_idINTEGERReferences partners.partner_id
user_idINTEGERInstacart customer identifier
order_dateDATEOrder date
statusVARCHAR(20)Order lifecycle status
basket_amountNUMERIC(10,2)Total value of the order basket
Tablespartnersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results