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
Rolling Retention Rates Across Sites
00:00
5 left

Rolling Retention Rates Across Sites

HardSQL · PostgreSQL

Problem

Verily Baseline study operations need to compare patient retention across clinical sites. A patient is retained for a visit date if the same patient has another visit at the same site within the following 30 calendar days.

Write a PostgreSQL query that calculates rolling 30-day retention by site and active visit date.

Requirements

  1. Count distinct active patients for each site and visit date.
  2. Count distinct retained patients whose subsequent visit occurs after the active date and within 30 days.
  3. Use a window function to calculate the retention rate across the current date and the previous 29 calendar days for each site.
  4. Return only rows with a non-null visit date, ordered by site and date. Round the retention rate to four decimal places.

Schema

sites
ColumnTypeDescription
site_idPKINTEGERClinical site identifier
site_nameVARCHAR(100)Clinical site name
regionVARCHAR(50)Geographic region of the site
patient_visits
ColumnTypeDescription
visit_idPKINTEGERUnique visit identifier
site_idINTEGERSite where the visit occurred
patient_idINTEGERPatient identifier
visit_dateDATEDate of the patient visit
Tablessitespatient_visits
Interviewer

Your question is Rolling Retention Rates Across Sites. 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.