Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products and Repeat Purchases

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

Your question is Top Products and Repeat Purchases. 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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results