Practice Free
EasyFundamentals

Critical Inpatient ICU Bed Priority Logic

Frequently Asked By:AmazonGoogleMeta
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

Filter the `inpatient_vitals` table for patients who meet either of these critical clinical conditions: 1. `oxygen_pct` is strictly less than **90** 2. **OR** (`systolic_bp` is strictly greater than **180** **AND** `heart_rate` is strictly greater than **120**) Return: 1. `patient_id` 2. `ward_unit` 3. `oxygen_pct` 4. `systolic_bp` 5. `heart_rate` Order results by `oxygen_pct` ascending, breaking ties by `patient_id` ascending.

Why Technical Interviewers Ask This

The chief medical officer needs to audit patient transfer eligibility for Intensive Care Units (ICU). Patients require immediate ICU transfer if they have oxygen saturation below 90% OR (systolic blood pressure above 180 AND heart rate above 120).

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 fundamentals constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        patient_id,
        ward_unit,
        oxygen_pct,
        systolic_bp,
        heart_rate
      FROM inpatient_vitals
      WHERE oxygen_pct < 90
         OR (systolic_bp > 180 AND heart_rate > 120)
      ORDER BY oxygen_pct ASC, patient_id ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Fundamentals. It meets the target criteria: Inpatients in clinical distress requiring ICU escalation, sorted by lowest oxygen saturation.

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 Amazon and Google interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Fundamentals 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