Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Weekly Shopper Retention Rate

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

Your question is Weekly Shopper Retention Rate. 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

ShopKart wants to measure how many shoppers return in the following week after making a purchase. Write a SQL query to calculate week-over-week shopper retention.

A shopper is considered retained in week W if they made at least one completed order in week W and also made at least one completed order in week W + 1.

Requirements

  1. Use only completed orders.
  2. Aggregate activity by shopper and calendar week using DATE_TRUNC('week', order_date).
  3. For each week, return:
    • the week start date
    • the number of active shoppers in that week
    • the number of those shoppers who returned the next week
    • the retention rate as retained_shoppers / active_shoppers, rounded to 4 decimals
  4. Exclude the final week in the dataset if there is no following week to evaluate retention against.
  5. Order results by week start date ascending.

Schema

shoppers
ColumnTypeDescription
shopper_idPKINTUnique shopper identifier
shopper_nameVARCHAR(100)Full name of the shopper
signup_dateDATEDate the shopper signed up
regionVARCHAR(50)Shopper region
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
shopper_idINTShopper who placed the order
order_dateDATEDate the order was placed
order_statusVARCHAR(20)Order lifecycle status
order_totalNUMERIC(10,2)Total order amount
Tablesshoppersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results