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
Retention Rate for a Series
00:00
5 left

Retention Rate for a Series

HardSQL · PostgreSQL

Problem

Prime Video wants to measure whether viewers return to watch the same series after their initial viewing. Write a PostgreSQL query to calculate the 30-day retention rate for users who first watched series 101, The Peripheral, during January 2025.

Requirements

  1. Define the cohort as distinct users whose first qualifying view of series 101 occurred from 2025-01-01 through 2025-01-31.
  2. Count a user as retained if they watched series 101 again more than 0 and no more than 30 days after their first qualifying view.
  3. Return the cohort size, retained-user count, and retention rate as a percentage rounded to two decimal places.
  4. Prevent multiple follow-up views from counting a user more than once.

Output details

  • Return one row with columns cohort_users, retained_users, and retention_rate_pct, in that order.
  • Return NULL for retention_rate_pct when the cohort contains zero users.

Schema

users
ColumnTypeDescription
user_idPKINTPrime Video user identifier
regionVARCHAR(30)User region
series
ColumnTypeDescription
series_idPKINTSeries identifier
titleVARCHAR(150)Series title
genreVARCHAR(50)Series genre
watch_events
ColumnTypeDescription
event_idPKINTViewing event identifier
user_idINTViewer identifier referencing users
series_idINTWatched series identifier referencing series
watched_atTIMESTAMPTime of the viewing event
watch_secondsINTNumber of seconds watched
Tablesusersserieswatch_events
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables 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.