Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Cohort and Timeline Query

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

Your question is SQL Cohort and Timeline Query. Start with the requirements and the three 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

Business Context

Dana-Farber Cancer Institute's clinical analytics team is reviewing whether patients developed a specified condition after receiving a particular treatment.

Task

Write a PostgreSQL query to find every qualifying Treatment A and Condition B event pair. A diagnosis qualifies only when it occurs after Treatment A and no later than six calendar months after that treatment.

Requirements

  1. Join patient, treatment, and diagnosis records using patient_id.
  2. Restrict treatments to Treatment A and diagnoses to Condition B.
  3. Exclude same-day and pre-treatment diagnoses, include the exact six-month boundary, and order results by patient and treatment date.
  4. Return the patient identifiers and names, treatment date, and qualifying condition date.

Schema

patients
ColumnTypeDescription
patient_idPKINTEGERUnique patient identifier
patient_nameVARCHAR(100)Patient's display name
treatments
ColumnTypeDescription
treatment_idPKINTEGERUnique treatment record identifier
patient_idINTEGERPatient receiving the treatment
treatment_nameVARCHAR(100)Name of the treatment administered
treatment_dateDATEDate the treatment was administered
diagnoses
ColumnTypeDescription
diagnosis_idPKINTEGERUnique diagnosis record identifier
patient_idINTEGERPatient receiving the diagnosis
condition_nameVARCHAR(100)Name of the diagnosed condition
condition_dateDATEDate the condition was diagnosed
Tablespatientstreatmentsdiagnoses
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results