Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detect Post-Launch Customer Behavior Shifts

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

Your question is Detect Post-Launch Customer Behavior Shifts. 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

NovaCart launched a new product on 2024-04-01 and wants to identify customers whose purchasing behavior changed afterward. Write a SQL query to compare each customer's activity in the 30 days before launch vs. the 30 days after launch.

Requirements

  1. For each customer, calculate pre_orders, post_orders, pre_spend, and post_spend using orders in the windows 2024-03-02 to 2024-03-31 and 2024-04-01 to 2024-04-30.
  2. Return only customers who had at least 1 order in both periods.
  3. Flag a customer as changed if either order count changed by at least 2 or total spend changed by at least 100.
  4. Include the customer's segment and sort by absolute spend change descending, then customer_id.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer full name
segmentVARCHAR(50)Customer segment
signup_dateDATEDate the customer signed up
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
order_dateDATEDate the order was placed
order_amountDECIMAL(10,2)Total order amount
channelVARCHAR(30)Purchase channel
product_launches
ColumnTypeDescription
launch_idPKINTUnique launch identifier
product_nameVARCHAR(100)Product name
launch_dateDATELaunch date for the product
Tablescustomersordersproduct_launches
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results