Home Cases Blog About

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.

The tables

TableColumns
suspect_infoid, case_id, name, role, relation, location, alibi_time
eventsid, case_id, suspect_id, suspect_name, event_type, details, event_timestamp
suspect_reasonid, 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

Case 5: Evil Under the Sun · Medium · uses WHERE, CASE

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
Run it in Case 5 (question 2 of 5)

2. Identify Passengers

Case 8: Death on the Nile · Hard · uses WHERE, ORDER BY, LIKE, CASE

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
Run it in Case 8 (question 1 of 5)

3. Analyze Testimony Consistency

Case 9: Five Little Pigs · Hard · uses WHERE, ORDER BY, CASE, ROW_NUMBER, LAG, IS NULL

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
Run it in Case 9 (question 2 of 5)

More SQL exercises

Play the cases in order →