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
Latest Lab Result SQL
00:00
5 left

Latest Lab Result SQL

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the latest lab result for each patient in a study, partitioned by patient ID and ordered by visit date.

Use the study identified by study_code = 'PMG-ONC-101'. Include enrolled patients without a lab result, and use result_id as the tie-breaker when a patient has multiple results on the same visit date.

Output

  1. One row per enrolled patient, ordered by patient_id ascending.
  2. Columns: patient_id, visit_date, lab_test, and result_value.
  3. Patients without results should have NULL values for the lab result columns.

Schema

studies
ColumnTypeDescription
study_idPKINTUnique study identifier
study_codeVARCHAR(30)Study code used to identify the study
study_nameVARCHAR(120)Study name
study_patients
ColumnTypeDescription
study_patient_idPKINTUnique enrollment record identifier
study_idINTReferenced study
patient_idVARCHAR(20)Patient identifier
enrolled_onDATEStudy enrollment date
lab_results
ColumnTypeDescription
result_idPKINTUnique lab result identifier
patient_idVARCHAR(20)Patient associated with the result
visit_dateDATEDate of the patient visit
lab_testVARCHAR(80)Name of the laboratory test
result_valueVARCHAR(40)Reported laboratory result value
Tablesstudiesstudy_patientslab_results
Interviewer

Your question is Latest Lab Result SQL. Start with the requirements and the three 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.