SQL CASE WHEN Exercises
3 exercises with answers, easiest first.
CASE WHEN adds your own labels to rows, such as marking an alibi as confirmed or unconfirmed. It is the IF statement of SQL.
- 3 exercises, each from a real murder case
- 31% 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. Group Suspects by Their Alibi Timings
Use the suspect_info table to label each guest's alibi as 'At-Murder' (exactly at 11:45 AM), 'Pre-Murder' (before 11:45 AM) or 'Post-Murder' (after 11:45 AM).
filter by case_id = 5 in your query to focus only on this investigation. Times are stored as HH:MM:SS, so 11:45 AM is '11:45:00'.
20% of players got this right on their first query (25 players, median 3 tries).
Hint
- Alibi times are in HH:MM:SS format. Ex. 11:45AM is '11:45:00'.
- The murder occurred at exactly 11:45:00. Classify each suspect as 'At-Murder', 'Pre-Murder', or 'Post-Murder'.
- Use the CASE expression to classify based on alibi_time.
Show the answer
SELECT
name,
CASE
WHEN alibi_time = '11:45:00' THEN 'At-Murder'
WHEN alibi_time < '11:45:00' THEN 'Pre-Murder'
ELSE 'Post-Murder'
END AS time_bucket
FROM suspect_info
WHERE case_id = 5
2. Identify Passengers
List each passenger's name and categorize their relationship to Linnet as 'Family', 'Professional', or 'Other'. Order alphabetically by name.
filter by case_id = 8 in your query to focus only on the current investigation. Use CASE WHEN with ILIKE for pattern matching.
Show the answer
SELECT
name,
CASE
WHEN relation ILIKE '%husband%' OR relation ILIKE '%cousin%' OR relation ILIKE '%family%' THEN 'Family'
WHEN relation ILIKE '%maid%' OR relation ILIKE '%doctor%' OR relation ILIKE '%lawyer%' THEN 'Professional'
ELSE 'Other'
END AS relationship_category
FROM suspect_info
WHERE case_id = 8
ORDER BY name
3. 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