Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Churn Cohorts
00:00
5 left

SQL for Churn Cohorts

HardSQL · PostgreSQL

Problem

Write a SQL query to analyze customer churn by cohort and identify the top drivers of churn.

Use customer sign-up cohorts through June 2024 and consider churn events from the sign-up date through September 30, 2024. For each churn driver, include the number of affected customers and their average completed orders before churn.

Output

  1. One row per cohort and churn driver, limited to the top three drivers within each cohort.
  2. Columns: cohort_month, churn_driver, churned_customers, cohort_customers, churn_rate_pct, avg_pre_churn_orders, and driver_rank.
  3. Sort by cohort_month ascending and driver_rank ascending, with churn count, average orders, and driver name resolving ties.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
signup_dateDATECustomer signup date
churn_events
ColumnTypeDescription
churn_event_idPKINTUnique churn event identifier
customer_idINTCustomer associated with the churn event
churn_dateDATEDate the customer churned
churn_reasonVARCHAR(100)Reported primary churn driver
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
order_dateDATEDate the order was placed
order_amountNUMERIC(10,2)Order subtotal
Tablescustomerschurn_eventsorders
Interviewer

Your question is SQL for Churn Cohorts. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.