Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Rolling Churn With Seasonality
00:00
5 left

SQL Rolling Churn With Seasonality

HardSQL · PostgreSQL

Problem

Write a SQL query to identify customer churn patterns over a rolling three-month window, accounting for seasonal variations.

Use the provided customer, monthly activity, and seasonal factor data. Evaluate the latest available month, treating missing activity as inactivity and preserving customers with no activity.

Output

  1. One row per customer for the latest available month.
  2. Columns: customer_id, customer_name, window_end, rolling_active_months, rolling_spend, seasonal_expected_active_months, seasonally_adjusted_activity_ratio, and churn_pattern.
  3. Classify customers as churned, at_risk, or retained; sort by customer_id ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
signup_dateDATEDate the customer joined
regionVARCHAR(50)Customer geographic region
monthly_customer_activity
ColumnTypeDescription
activity_idPKINTUnique monthly activity record
customer_idINTReferenced customer
activity_monthDATEFirst day of the activity month
is_activeBOOLEANWhether the customer was active that month
monthly_spendNUMERIC(12,2)Customer spend during the month
seasonal_factors
ColumnTypeDescription
month_numberPKINTCalendar month number from 1 through 12
month_nameVARCHAR(20)Calendar month name
expected_active_rateNUMERIC(5,2)Expected fraction of customers active in the month
Tablescustomersmonthly_customer_activityseasonal_factors
Interviewer

Your question is SQL Rolling Churn With Seasonality. 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.