Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Slow Patient Queries

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results