Practice Free
MediumAggregations

Emergency Room Average Door-to-Doctor Wait Times

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

Group records in `er_patient_encounters` by `acuity_level`. Calculate: 1. `acuity_level` 2. `total_patients`: Total patients in this acuity tier 3. `avg_wait_minutes`: Average `wait_minutes` rounded to 1 decimal place 4. `max_wait_minutes`: Maximum recorded `wait_minutes` Order results by `acuity_level` ascending.

Why Technical Interviewers Ask This

Hospital clinical operations track Emergency Department triage wait times in minutes to ensure high-acuity patients are seen well within medical protocol thresholds.

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

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        acuity_level,
        COUNT(*) AS total_patients,
        ROUND(AVG(wait_minutes), 1) AS avg_wait_minutes,
        MAX(wait_minutes) AS max_wait_minutes
      FROM er_patient_encounters
      GROUP BY acuity_level
      ORDER BY acuity_level ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Average and maximum ER wait time by acuity tier.

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 Aggregations 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