Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Month-Over-Month Retention with LAG/LEAD
00:00
5 left

Month-Over-Month Retention with LAG/LEAD

MediumSQL · PostgreSQL

Problem

Capgemini Invent wants to monitor month-over-month customer retention for its client portal. A retained customer is an eligible customer with portal activity in both the current month and the immediately preceding month.

Write a PostgreSQL query to calculate and compare monthly retention from January through April 2025.

Requirements

  1. Include only active customers whose client account has portal_enabled = TRUE.
  2. Count active customers for each calendar month, including months with no activity.
  3. Use LAG to identify prior-month activity for each customer and to retrieve the previous month's active-customer count and retention rate.
  4. Calculate retention as retained customers divided by the previous month's active customers, returning NULL when no prior month exists.
  5. Return the monthly retention rate and its change from the previous month's rate, ordered chronologically.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
account_idINTClient account identifier
customer_nameVARCHAR(100)Customer name
statusVARCHAR(20)Customer lifecycle status
client_accounts
ColumnTypeDescription
account_idPKINTUnique client account identifier
account_nameVARCHAR(120)Client account name
portal_enabledBOOLEANWhether portal activity is eligible for retention reporting
portal_activity
ColumnTypeDescription
activity_idPKINTUnique activity event identifier
customer_idINTCustomer who generated the activity
activity_dateDATEDate of portal activity
Tablescustomersclient_accountsportal_activity
Interviewer

Your question is Month-Over-Month Retention with LAG/LEAD. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.