Practice Free
MediumJoins

Inpatient Primary Attending Physician Roster

Frequently Asked By:AppleGoogleAmazon
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

Join the `hospital_patients` table with `physicians` on `attending_doctor_id = doctor_id`. Return: 1. `p.patient_mrn`: Medical record number 2. `p.patient_name` 3. `p.ward_room` 4. `d.doctor_name` 5. `d.specialty` Only include patients whose status is `'admitted'`. Order results by `p.ward_room` ascending, then by `p.patient_name` ascending.

Why Technical Interviewers Ask This

During daily morning hospital rounds, clinical operations generates a roster cross-referencing active inpatients with their assigned attending physician and clinical specialty.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass joins constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        p.patient_mrn,
        p.patient_name,
        p.ward_room,
        d.doctor_name,
        d.specialty
      FROM hospital_patients p
      JOIN physicians d ON p.attending_doctor_id = d.doctor_id
      WHERE p.status = 'admitted'
      ORDER BY p.ward_room ASC, p.patient_name ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Joins. It meets the target criteria: Roster of admitted inpatients with attending physician details, sorted by ward room.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Apple and Google interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Joins queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions