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
Complex Joins on Normalized Data
00:00
5 left

Complex Joins on Normalized Data

HardSQL · PostgreSQL

Problem

How do you handle complex joins when extracting data from a highly normalized schema?

Write a PostgreSQL query using the provided normalized tables. Include qualifying encounters from January 1 through March 31, 2025, and exclude cancelled or records with an unspecified status.

Output

  1. One row per patient and facility combination, including patients without qualifying encounters.
  2. Return patient_id, patient_name, facility_id, facility_name, encounter_count, latest_encounter_date, and total_charges.
  3. Use zero counts and charges for patients without qualifying encounters, label missing facility names as Unmatched facility, and order by patient_id then facility ID, with unmatched rows last.

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
patient_nameVARCHAR(100)Patient display name
encounters
ColumnTypeDescription
encounter_idPKINTUnique encounter identifier
patient_idINTReferenced patient
facility_idINTReferenced facility, when known
encounter_dateDATEDate of the encounter
statusVARCHAR(20)Encounter lifecycle status
total_chargesDECIMAL(12,2)Total charges recorded for the encounter
facilities
ColumnTypeDescription
facility_idPKINTUnique facility identifier
facility_nameVARCHAR(120)Facility display name
Tablespatientsencountersfacilities
Interviewer

Your question is Complex Joins on Normalized Data. 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.