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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Calculate churn rate from a subscription table
00:00
5 left

Calculate churn rate from a subscription table

MediumSQL · PostgreSQL

Problem

Calculate Churn rate using company level table with subscriptiondate and terminationdate?

Asked in the 1:1 technical stage. The table is company(companyid, signupdate, subscriptiondate, terminationdate), and churn rate is customers that churned in a period divided by total customers at the start of that period.

Output

  1. Return one row per calendar month from the earliest subscription month through the latest relevant month.
  2. Include period_month, churned_customers, customers_at_start, and churn_rate.
  3. Include companies active at the start of each month and terminations occurring during that month. Order by period_month ascending.

Output details

  • Treat a company as active at month start when subscriptiondate is on or before period_month and terminationdate is NULL or on or after it.
  • Count terminations from the inclusive first day of period_month through the exclusive first day of the following month.
  • Use the later of the latest subscriptiondate and latest non-NULL terminationdate to determine the final month.
  • Round churn_rate to 4 decimal places and return NULL when customers_at_start is zero.

Schema

company
ColumnTypeDescription
companyidPKINTUnique company identifier
signupdateDATEDate the company signed up
subscriptiondateDATEDate the company subscription began
terminationdateDATEDate the company subscription terminated
Tablescompany
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

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.