SQL Window Function Exercises
4 exercises with answers, easiest first.
Window functions look across rows without collapsing them. ROW_NUMBER numbers each suspect's events in order. LAG compares a statement with the one before it.
- 4 exercises, each from a real murder case
- 35% of players solve these on the first query
- Answers and hints are hidden until you click
The tables
| Table | Columns |
|---|---|
| suspect_info | id, case_id, name, role, relation, location, alibi_time |
| events | id, case_id, suspect_id, suspect_name, event_type, details, event_timestamp |
| suspect_reason | id, suspect_id, case_id, reason_type, summary, detail |
PostgreSQL. Times in alibi_time are stored as HH:MM:SS.
The exercises
1. Determine the Sequence of Deaths
Use the events table to list each victim's name and assign a death order (numbered 1, 2, 3, etc.) based on the event_timestamp, ordered chronologically.
filter by case_id = 7 in your query to focus only on the current investigation. Use ROW_NUMBER() window function to assign the sequence.
Hint
- Use ROW_NUMBER() OVER (ORDER BY event_timestamp) to assign sequential numbers.
- Filter for death events using event_type IN ('poisoned', 'murder').
- Order the final results by death_order to see the sequence.
Show the answer
SELECT
suspect_name AS name,
ROW_NUMBER() OVER (ORDER BY event_timestamp) AS death_order
FROM events
WHERE case_id = 7
AND event_type IN ('poisoned', 'murder')
ORDER BY death_order
2. Analyze Testimony Consistency
Compare each suspect's consecutive testimonies to identify contradictions. Show suspect_name, testimony_number, current_detail, previous_detail, and is_consistent. Order by suspect name and testimony number.
filter by case_id = 9 in your query. Use ROW_NUMBER(), LAG() window functions with CASE WHEN for comparison.
Show the answer
SELECT
suspect_name,
testimony_number,
current_detail,
previous_detail,
is_consistent
FROM (
SELECT
suspect_name,
ROW_NUMBER() OVER (PARTITION BY suspect_name ORDER BY event_timestamp) AS testimony_number,
details AS current_detail,
LAG(details) OVER (PARTITION BY suspect_name ORDER BY event_timestamp) AS previous_detail,
CASE
WHEN LAG(details) OVER (PARTITION BY suspect_name ORDER BY event_timestamp) IS NULL THEN TRUE
WHEN LAG(details) OVER (PARTITION BY suspect_name ORDER BY event_timestamp) = details THEN TRUE
ELSE FALSE
END AS is_consistent
FROM events
WHERE case_id = 9 AND event_type = 'testimony'
) sub
WHERE previous_detail IS NOT NULL
ORDER BY suspect_name, testimony_number
3. Timeline Gaps - Who Has Unaccounted Time?
Calculate time gaps between consecutive events for each suspect. Find gaps greater than 60 minutes. Show suspect_name, first event, next event, and gap minutes. Order by gap descending.
filter by case_id = 9 in your query to focus only on the current investigation. Use LEAD() window function with EXTRACT(EPOCH FROM ...) for time difference.
Show the answer
SELECT
suspect_name,
event_timestamp AS first_event,
next_event,
gap_minutes
FROM (
SELECT
suspect_name,
event_timestamp,
LEAD(event_timestamp) OVER (PARTITION BY suspect_name ORDER BY event_timestamp) AS next_event,
EXTRACT(EPOCH FROM (LEAD(event_timestamp) OVER (PARTITION BY suspect_name ORDER BY event_timestamp) - event_timestamp)) / 60 AS gap_minutes
FROM events
WHERE case_id = 9
) sub
WHERE gap_minutes > 60
ORDER BY gap_minutes DESC
4. Sequence of "accidents"
The deaths looked like accidents. Use the events table to list each victim (from details), the event_type (e.g. poisoned, hit-and-run, pushed), and event_timestamp. Add a column death_order (1, 2, 3, …) using ROW_NUMBER() over event_timestamp. Filter by case_id = 17 and only fatal event types. Order by event_timestamp.
filter by case_id = 17 and event_type IN ('poisoned','hit-and-run','pushed','murder').
Hint
- Use details AS victim_name, event_type, event_timestamp.
- Use ROW_NUMBER() OVER (ORDER BY event_timestamp) AS death_order.
- Filter with WHERE case_id = 17 and event_type IN ('poisoned','hit-and-run','pushed','murder').
Show the answer
SELECT details AS victim_name, event_type, event_timestamp,
ROW_NUMBER() OVER (ORDER BY event_timestamp) AS death_order
FROM events
WHERE case_id = 17
AND event_type IN ('poisoned', 'hit-and-run', 'pushed', 'murder')
ORDER BY event_timestamp