MediumFundamentals
Hospital Triage: Elevated Systolic Blood Pressure
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.
Problem Statement
Write a query to retrieve all patient vital readings from `triage_vitals` where `systolic_bp >= 140`.
Include a computed column named `hypertension_stage`:
- If `systolic_bp >= 180`, classify as `'CRISIS'`
- Otherwise, classify as `'STAGE_2'`
Return:
1. `patient_id`
2. `systolic_bp`
3. `diastolic_bp`
4. `hypertension_stage`
Order results by `systolic_bp` descending, then by `patient_id` ascending.
Why Technical Interviewers Ask This
Emergency triage clinical systems continuously evaluate vitals recorded at triage check-in to classify hypertension severity for attending nursing staff.
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 SQLSELECT
patient_id,
systolic_bp,
diastolic_bp,
CASE
WHEN systolic_bp >= 180 THEN 'CRISIS'
ELSE 'STAGE_2'
END AS hypertension_stage
FROM triage_vitals
WHERE systolic_bp >= 140
ORDER BY systolic_bp DESC, patient_id ASC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Fundamentals. It meets the target criteria: Patient triage records with elevated systolic readings classified by crisis urgency.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 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.