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
Window Functions for Churn
00:00
5 left

Window Functions for Churn

EasySQL · PostgreSQL

Problem

Business Context

Ancestry Marketing wants to identify users who showed repeated engagement with Ancestry surfaces but later became inactive. Use activity history as of April 1, 2025 to support churn analysis.

Task

Write a PostgreSQL query using a window function to return users with at least two recorded activities whose most recent activity was at least 30 days before April 1, 2025.

Requirements

  1. Use ROW_NUMBER() to identify each user's most recent activity.
  2. Return the user name, marketing channel, latest activity date, latest Ancestry surface, total activity count, and inactivity days.
  3. Exclude users with fewer than two activities and users whose latest activity was within the previous 30 days.
  4. Order results by inactivity days descending, then user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique user identifier
full_nameVARCHAR(100)User's full name
marketing_channelVARCHAR(50)Acquisition channel attributed to the user
activity_events
ColumnTypeDescription
activity_idPKINTEGERUnique activity identifier
user_idINTEGERReferences users.user_id
activity_dateDATEDate on which the activity occurred
surfaceVARCHAR(80)Ancestry surface used during the activity
event_typeVARCHAR(50)Recorded activity type
Tablesusersactivity_events
Interviewer

Your question is Window Functions for Churn. Start with the requirements and the two 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.