Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Churn

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Window Functions for Churn. Start with the requirements and the two tables on the right.

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.

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results