0
← Exercises/
Window Functions•6 of 14
Window FunctionsAdvancedPRO

Patient ICU Heart Rate Spike Detection with LAG()

Scenario

ICU monitoring systems continuously evaluate bedside telemetry. Sudden increases of 20+ beats per minute (BPM) between consecutive readings trigger cardiac telemetry alarms.

Task
For each vital reading in `icu_telemetry`, compute the heart rate change compared to the patient's immediately preceding reading. Calculate: 1. `patient_id` 2. `reading_time` 3. `heart_rate` 4. `prev_heart_rate`: Heart rate from the preceding reading using `LAG()` partitioned by `patient_id` 5. `bpm_delta`: (`heart_rate - LAG(heart_rate) OVER (PARTITION BY patient_id ORDER BY reading_time)`) Order results by `patient_id` ascending, then by `reading_time` ascending.
Expected Output Format

Patient telemetry readings showing previous heart rate and BPM delta.

Input Tables & Sample Data0 tables
Loading database schema...
Need a hint?
query.sql|Local Sandbox
Loading SQL editor...
Click "Run" or press ⌘ + Enter to execute your query

About the “Patient ICU Heart Rate Spike Detection with LAG()” SQL Problem

Business Scenario

ICU monitoring systems continuously evaluate bedside telemetry. Sudden increases of 20+ beats per minute (BPM) between consecutive readings trigger cardiac telemetry alarms.

Expected Output & Criteria

Patient telemetry readings showing previous heart rate and BPM delta.

Database Table Schema (PostgreSQL DDL)

PostgreSQL 16 WASM

This challenge executes directly against the following relational schema in your browser:

CREATE TABLE icu_telemetry (
        reading_id INT PRIMARY KEY,
        patient_id INT NOT NULL,
        reading_time TIMESTAMP NOT NULL,
        heart_rate INT NOT NULL
      );

Target SQL Clauses & Operators

LAGOVERPARTITION BY

Want more Window Functions practice?

Explore the full syllabus of 14 exercises, syntax guides, and interview pitfalls.

View Window Functions Track→