Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
Optimize Slow Patient Queries
00:00
5 left

Optimize Slow Patient Queries

HardSQL · PostgreSQL

Problem

How do you optimize a slow-running SQL query that processes millions of rows of patient records at Medpace?

Write a PostgreSQL query that returns the active patients in study 101, their latest completed visit through January 31, 2025, and their serious or critical adverse-event count during the preceding 30 days. The result should preserve eligible patients without matching visits or events.

Output

  1. One row per active study-101 patient, with site_id, site_name, patient_id, latest_completed_visit, serious_event_count, and site_rank
  2. Include patients with no qualifying visit or event, representing missing values appropriately
  3. Rank patients within each site by serious-event count descending, then patient ID ascending
  4. Order the final rows by site_id, then patient_id

Schema

clinical_sites
ColumnTypeDescription
site_idPKINTClinical site identifier
study_idINTStudy identifier
site_nameVARCHAR(80)Clinical site name
patients
ColumnTypeDescription
patient_idPKINTPatient identifier
study_idINTStudy identifier
site_idINTAssigned clinical site
enrollment_statusVARCHAR(30)Current enrollment status
patient_visits
ColumnTypeDescription
visit_idPKINTVisit identifier
patient_idINTPatient associated with the visit
visit_dateDATEScheduled or completed visit date
visit_statusVARCHAR(40)Visit workflow status
adverse_events
ColumnTypeDescription
event_idPKINTAdverse event identifier
patient_idINTPatient associated with the event
event_dateDATEDate the event was recorded
severityVARCHAR(20)Event severity classification
Tablesclinical_sitespatientspatient_visitsadverse_events
Interviewer

Your question is Optimize Slow Patient Queries. Start with the requirements and the four 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.