Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Retention with Window Functions

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

Your question is Rolling Retention with Window Functions. 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

Lyft wants to measure whether riders who take a completed ride remain active. Write a PostgreSQL query that calculates rolling 30-day retention for active Lyft riders.

For each activity date from January 1 through January 4, 2024, an active rider is retained if the same rider completes at least one later ride within the following 30 days, including day 30 and excluding the original activity date.

Requirements

  1. Join rides to riders and include only riders whose account is active and rides whose status is completed.
  2. Use a window function to deduplicate multiple rides by the same rider on the same date.
  3. Return each activity date, active rider count, retained rider count, and retention rate as a percentage, rounded to two decimals.
  4. Order the results chronologically. Do not count a rider more than once in either metric.

Schema

riders
ColumnTypeDescription
rider_idPKINTUnique Lyft rider identifier
rider_nameVARCHAR(100)Rider display name
account_statusVARCHAR(20)Current rider account status
rides
ColumnTypeDescription
ride_idPKINTUnique ride identifier
rider_idINTRider who took the ride
ride_dateDATECalendar date of the ride
ride_statusVARCHAR(20)Ride outcome
completed_atTIMESTAMPTimestamp when the ride completed
Tablesridersrides
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results