Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Weekly Shopper Retention Rate
00:00
5 left

Weekly Shopper Retention Rate

MediumSQL · PostgreSQL

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.