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
Weekly Retention Curve for Facebook
00:00
5 left

Weekly Retention Curve for Facebook

MediumSQL · PostgreSQL

Problem

Facebook wants a weekly retention curve showing how many users active in week N were also active in week N+1. Write a PostgreSQL query to calculate week-over-week retention.

Requirements

  1. Treat a user as active in a week if they generated at least one event in that calendar week.
  2. Deduplicate multiple events from the same user in the same week.
  3. For each week_start, compute:
    • the number of active users in that week
    • the number of those users also active in the following week
    • the retention rate as retained_users / active_users, rounded to 4 decimal places
  4. Return one row per week that has at least one active user.
  5. Order results by week_start ascending.

Schema

facebook_events
ColumnTypeDescription
event_idPKINTUnique event row identifier
user_idINTFacebook user identifier
event_nameVARCHAR(50)Type of Facebook event
event_timeTIMESTAMPTimestamp when the event occurred
device_typeVARCHAR(20)Device used for the event
Tablesfacebook_events
Interviewer

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