Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Top Products and Repeat Purchases
00:00
5 left

Top Products and Repeat Purchases

HardSQL · PostgreSQL

Problem

Business context

Vanguard's digital commerce analytics team wants a recent sales snapshot for products and customer retention signals.

Task

As of 2025-02-01, write one PostgreSQL query that returns the top three products by sales during the inclusive 30-day period ending on that date, and identifies customers who made another purchase within seven days of an earlier purchase.

Requirements

  1. Join purchases to products and calculate product sales as quantity * unit_price.
  2. Filter product sales to the inclusive 30-day period and rank products by total sales, returning only the top three.
  3. Use purchase dates to identify customers with a later purchase no more than seven days after an earlier purchase.
  4. Return both result types in one result set, with result_type set to TOP_PRODUCT or REPEAT_CUSTOMER.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer full name
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
purchases
ColumnTypeDescription
purchase_idPKINTUnique purchase identifier
customer_idINTCustomer who made the purchase
product_idINTProduct purchased
purchase_dateDATEDate of purchase
quantityINTNumber of units purchased
unit_priceNUMERIC(10,2)Price per unit at purchase time
Tablescustomersproductspurchases
Interviewer

Your question is Top Products and Repeat Purchases. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.