HardBusiness Analytics High Frequency
All-Cause 30-Day Hospital Readmission Rate
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 calculate readmissions occurring between 1 and 30 days after initial inpatient discharge.
1. In a CTE `readmission_cases`, find hospital admissions (`a2`) for the same patient where `a2.admission_date > a1.discharge_date AND a2.admission_date <= a1.discharge_date + 30`.
2. In the outer query, calculate total index discharges and count of patients readmitted.
Return:
1. `total_index_discharges`: Count of all records in `inpatient_discharges`
2. `readmitted_count`: Count of unique patients with a 30-day readmission
3. `readmission_rate_pct`: `ROUND(readmitted_count * 100.0 / total_index_discharges, 1)`
(No ORDER BY needed for single-row summary).
Why Technical Interviewers Ask This
Hospital clinical quality committees track patient readmissions occurring within 30 days of prior inpatient discharge to assess post-acute care coordination.
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 business analytics constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLWITH index_count AS (
SELECT COUNT(*) AS total_index_discharges FROM inpatient_discharges
),
readmissions AS (
SELECT COUNT(DISTINCT d.patient_id) AS readmitted_count
FROM inpatient_discharges d
JOIN next_admissions a
ON d.patient_id = a.patient_id
AND a.admission_date > d.discharge_date
AND a.admission_date <= (d.discharge_date + 30)
)
SELECT
i.total_index_discharges,
r.readmitted_count,
ROUND(r.readmitted_count * 100.0 / i.total_index_discharges, 1) AS readmission_rate_pct
FROM index_count i
CROSS JOIN readmissions r;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Business Analytics. It meets the target criteria: Inpatient 30-day readmission metric summary.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 Business Analytics 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.