Home Cases Blog About

SQL JOIN Exercises

17 exercises with answers, easiest first.

A JOIN lines up rows from two tables. In these cases that means matching each suspect to their events or motives. LEFT JOIN keeps the suspects with no match, who are often the interesting ones.

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. Who never saw the victim alive?

Case 4: Murder in Mesopotamia · Medium · uses WHERE, LEFT JOIN, IS NULL

Use the suspect_info and events tables to list each suspect's name who has not seen the victim alive.

filter by case_id = 4 in your query to focus only on the current investigation. Some suspects have no 'seen_alive' event at all, so make sure your query keeps them. The victim is listed in suspect_info too, so leave her out.

19% of players got this right on their first query (59 players, median 8 tries).

Hint
  • Use Left Join on suspect_id and event_type = 'seen_alive' to find the alibi details.
  • Exclude the victim by adding role !='Victim'.
  • Identify those with no matching 'seen_alive' events by checking events.id IS NULL
Show the answer
SELECT si.name
FROM suspect_info AS si 
LEFT JOIN events AS e 
  ON e.suspect_id = si.id
  AND e.event_type = 'seen_alive' 
WHERE si.case_id = 4
AND si.role <> 'Victim'
AND e.id IS NULL
Run it in Case 4 (question 4 of 5)

2. Which Leonides family members have an explicit motive?

Case 6: Crooked House · Medium · uses WHERE, JOIN

Join the suspect_info and suspect_reason tables to list each Leonides family member's first name and motive(summary) for those with a recorded reason_type as 'Motive'.

filter by case_id = 6 in both tables to focus only on this investigation.

15% of players got this right on their first query (20 players, median 7 tries).

Show the answer
SELECT
  split_part(s.name, ' ', 1) AS first_name,
  r.summary AS motive_summary
FROM suspect_info AS s
JOIN suspect_reason AS r
  ON r.suspect_id = s.id
WHERE s.case_id = 6
  AND r.case_id = 6
  AND r.reason_type = 'Motive'
Run it in Case 6 (question 4 of 5)

3. What are each suspect's alibi details?

Case 4: Murder in Mesopotamia · Medium · uses WHERE, ORDER BY, JOIN

Use the suspect_info and events tables to show each suspect's name alongside their alibi details, ordered by their alibi time.

filter by case_id = 4 in your query to focus only on the current investigation.

11% of players got this right on their first query (75 players, median 6 tries).

Hint
  • Look for event_type = 'alibi' to find the alibi details.
Show the answer
SELECT si.name, e.details
FROM suspect_info AS si
JOIN events AS e
  ON e.suspect_id = si.id
WHERE si.case_id = 4
  AND e.event_type = 'alibi'
ORDER BY si.alibi_time
Run it in Case 4 (question 3 of 5)

4. Cross-Reference Accusations with Death Events

Case 7: And Then There Were None · Hard · uses WHERE, IN, JOIN

Use suspect_info and events tables to list each guest's name, their accusation (role), and their death event type (poisoned or murder). Join the tables to show which guests died and how.

filter by case_id = 7 in your query to focus only on the current investigation.

Hint
  • Use JOIN events ON events.suspect_id = suspect_info.id to link the tables.
  • Filter for death events using event_type IN ('poisoned', 'murder').
  • Select name, role, event_type from the joined tables.
Show the answer
SELECT
  si.name,
  si.role,
  e.event_type
FROM suspect_info AS si
JOIN events AS e
  ON e.suspect_id = si.id
WHERE si.case_id = 7
  AND e.event_type IN ('poisoned', 'murder')
Run it in Case 7 (question 3 of 5)

5. Find Guests Without Orchestration Events

Case 7: And Then There Were None · Hard · uses WHERE, LEFT JOIN, IS NULL

Use the suspect_info and events tables to list each guest's name who does NOT have an 'alibi' event type. This will show all the victims who were not orchestrating the killings.

filter by case_id = 7 in your query to focus only on the current investigation.

Hint
  • Use LEFT JOIN events ON events.suspect_id = suspect_info.id AND event_type = 'alibi' to find guests with alibi events.
  • Identify those with no matching 'alibi' events by checking events.id IS NULL.
  • Alternatively, you can use NOT EXISTS with a subquery.
Show the answer
SELECT si.name
FROM suspect_info AS si
LEFT JOIN events AS e
  ON e.suspect_id = si.id
  AND e.event_type = 'alibi'
WHERE si.case_id = 7
  AND e.id IS NULL
Run it in Case 7 (question 4 of 5)

6. Who Was Where?

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

Find pairs of suspects who were involved in events at the exact same timestamp. List suspect_1, suspect_2, and the shared event_timestamp, ordered by timestamp. Show each pair only once.

filter by case_id = 8 in your query to focus only on the current investigation. Use a self-join with inequality condition (a.suspect_name ).

Show the answer
SELECT
  a.suspect_name AS suspect_1,
  b.suspect_name AS suspect_2,
  a.event_timestamp
FROM events a
JOIN events b
  ON a.event_timestamp = b.event_timestamp
  AND a.case_id = b.case_id
  AND a.suspect_name < b.suspect_name
WHERE a.case_id = 8
ORDER BY a.event_timestamp
Run it in Case 8 (question 2 of 5)

7. Cross-Reference Alibis

Case 8: Death on the Nile · Hard · uses WHERE, LEFT JOIN, IS NULL

Find suspects whose alibi_time does NOT match any of their event_timestamps. Show name, alibi_time, and location for suspects with mismatched times.

filter by case_id = 8 in your query to focus only on the current investigation. Use LEFT JOIN with ::time conversion and filter on NULL.

Show the answer
SELECT
  si.name,
  si.alibi_time,
  si.location
FROM suspect_info si
LEFT JOIN events e
  ON si.name = e.suspect_name
  AND si.alibi_time = e.event_timestamp::time
  AND e.case_id = 8
WHERE si.case_id = 8
  AND e.id IS NULL
Run it in Case 8 (question 3 of 5)

8. Build the Testimony Chain

Case 9: Five Little Pigs · Hard · uses WHERE, ORDER BY, DISTINCT, LIKE, JOIN, CTE, RECURSIVE, UNION

Build a recursive chain showing who mentioned whom in testimonies. Show person, mentioned, and depth (chain level). Order by depth, then by person.

filter by case_id = 9 in your query. Use WITH RECURSIVE CTE to traverse the mention chain. Parse names from the details field using pattern matching.

Show the answer
WITH RECURSIVE testimony_chain AS (
  -- Base case: direct mentions
  SELECT 
    e.suspect_name AS person,
    si.name AS mentioned,
    1 AS depth
  FROM events e
  JOIN suspect_info si ON si.case_id = 9
  WHERE e.case_id = 9 
    AND e.event_type = 'testimony'
    AND e.details ILIKE '%' || si.name || '%'
    AND e.suspect_name != si.name
  
  UNION
  
  -- Recursive case: follow the chain
  SELECT 
    tc.mentioned AS person,
    si.name AS mentioned,
    tc.depth + 1 AS depth
  FROM testimony_chain tc
  JOIN events e ON e.suspect_name = tc.mentioned AND e.case_id = 9
  JOIN suspect_info si ON si.case_id = 9
  WHERE e.event_type = 'testimony'
    AND e.details ILIKE '%' || si.name || '%'
    AND e.suspect_name != si.name
    AND tc.depth < 3
)
SELECT DISTINCT person, mentioned, depth
FROM testimony_chain
ORDER BY depth, person, mentioned
Run it in Case 9 (question 1 of 5)

9. Which Suspects Have Recorded Events?

Case 10: Lord Edgware Dies · Medium · uses WHERE, ORDER BY, DISTINCT, JOIN

Use the suspect_info and events tables to list the names of suspects who have at least one event. Filter by case_id = 10, ordered by name.

filter by case_id = 10 in your query.

Hint
  • Use JOIN on suspect_info and events (e.g. si.name = e.suspect_name).
Show the answer
SELECT DISTINCT si.name
FROM suspect_info AS si
JOIN events AS e ON si.name = e.suspect_name AND e.case_id = 10
WHERE si.case_id = 10
ORDER BY si.name
Run it in Case 10 (question 3 of 5)

10. Which Guests Were Suspected of Past Murders?

Case 11: Cards on the Table · Medium · uses WHERE, ORDER BY, JOIN

Shaitana invited four guests he believed had gotten away with murder. Use the suspect_info and suspect_reason tables to list each suspect's name, role (profession), and the crime each may have committed. Filter by reason_type = 'crime'. Order by name.

filter by case_id = 11 in both tables.

Hint
  • Use JOIN between suspect_info and suspect_reason on suspect_info.id = suspect_reason.suspect_id.
  • Filter with reason_type = 'crime' and case_id = 11 in both tables.
  • Select name, role, and summary (aliased as suspected_crime) from suspect_reason.
Show the answer
SELECT si.name, si.role, sr.summary AS suspected_crime
FROM suspect_info si
JOIN suspect_reason sr ON si.id = sr.suspect_id AND sr.case_id = si.case_id
WHERE si.case_id = 11 AND sr.reason_type = 'crime'
ORDER BY si.name
Run it in Case 11 (question 3 of 5)

11. What motives or connections were recorded?

Case 12: The Clocks · Medium · uses WHERE, ORDER BY, JOIN

Various motives and connections were noted in the investigation. List each person who has a recorded reason and its summary. Use suspect_info and suspect_reason with case_id = 12. Show name and summary. Order by name.

filter by case_id = 12.

Hint
  • JOIN suspect_info and suspect_reason on suspect_id (and match case_id).
  • Select name and summary, filter by case_id = 12, ORDER BY name.
Show the answer
SELECT si.name, sr.summary
FROM suspect_info si
JOIN suspect_reason sr ON si.id = sr.suspect_id AND sr.case_id = si.case_id
WHERE si.case_id = 12
ORDER BY si.name
Run it in Case 12 (question 3 of 5)

12. Who was linked to the inheritance?

Case 12: The Clocks · Medium · uses WHERE, ORDER BY, JOIN

The victim had known the first Mrs. Bland; the inheritance went to the second. List everyone tied to the inheritance in the records. Use suspect_info and suspect_reason with case_id = 12 and reason_type = 'inheritance'. Show name and summary. Order by name.

filter by case_id = 12 and reason_type = 'inheritance'.

Hint
  • Same JOIN as Q3, plus WHERE sr.reason_type = 'inheritance'.
  • Show name and summary, ORDER BY name.
Show the answer
SELECT si.name, sr.summary
FROM suspect_info si
JOIN suspect_reason sr ON si.id = sr.suspect_id AND sr.case_id = si.case_id
WHERE si.case_id = 12 AND sr.reason_type = 'inheritance'
ORDER BY si.name
Run it in Case 12 (question 4 of 5)

13. Who had a recorded motive?

Case 13: Appointment with Death · Hard · uses WHERE, ORDER BY, JOIN

List each person with a recorded motive: name, reason_type, and summary. Use suspect_info and suspect_reason with case_id = 13. Order by name.

filter by case_id = 13.

Hint
  • JOIN suspect_info and suspect_reason on suspect_id (and match case_id).
  • Select name, reason_type, and summary. ORDER BY name.
Show the answer
SELECT si.name, sr.reason_type, sr.summary
FROM suspect_info si
JOIN suspect_reason sr ON si.id = sr.suspect_id AND sr.case_id = si.case_id
WHERE si.case_id = 13
ORDER BY si.name
Run it in Case 13 (question 3 of 5)

14. Whose motive was tied to the victim's past?

Case 13: Appointment with Death · Hard · uses WHERE, ORDER BY, LIKE, JOIN

Mrs. Boynton was once a prison warden; the killer had a secret she could expose. Find suspects whose motive summary refers to the victim's past, prison, warden, or exposure. Use suspect_info and suspect_reason with case_id = 13. Show name and summary. Order by name.

filter by case_id = 13 and use ILIKE on summary (e.g. '%prison%', '%warden%', '%expose%').

Hint
  • Same JOIN as Q3. Add WHERE sr.summary ILIKE '%prison%' OR sr.summary ILIKE '%warden%' OR sr.summary ILIKE '%expose%'.
  • Show name and summary, ORDER BY name.
Show the answer
SELECT si.name, sr.summary
FROM suspect_info si
JOIN suspect_reason sr ON si.id = sr.suspect_id AND sr.case_id = si.case_id
WHERE si.case_id = 13
  AND (sr.summary ILIKE '%prison%' OR sr.summary ILIKE '%warden%' OR sr.summary ILIKE '%expose%')
ORDER BY si.name
Run it in Case 13 (question 4 of 5)

15. Who appears in the most events?

Case 17: Murder is Easy · Hard · uses WHERE, ORDER BY, COUNT, GROUP BY, JOIN

Use suspect_info and events to list each suspect's name, role, and the number of event rows they appear in (event_count). Include only suspects who have at least one event. Order by event_count descending, then name.

filter by case_id = 17. Join on suspect_info.name = events.suspect_name.

Hint
  • JOIN suspect_info and events on si.name = e.suspect_name and si.case_id = e.case_id.
  • Use GROUP BY si.name, si.role and COUNT(*) AS event_count.
  • ORDER BY event_count DESC, name.
Show the answer
SELECT si.name, si.role, COUNT(*) AS event_count
FROM suspect_info si
JOIN events e ON e.suspect_name = si.name AND e.case_id = si.case_id
WHERE si.case_id = 17
GROUP BY si.name, si.role
ORDER BY event_count DESC, si.name
Run it in Case 17 (question 3 of 5)

16. Whose motive points to revenge or framing?

Case 17: Murder is Easy · Hard · uses WHERE, ORDER BY, LIKE, JOIN

Use suspect_info and suspect_reason to find suspects whose motive summary mentions revenge, framing, engagement, or Whitfield. Use ILIKE with patterns such as '%revenge%', '%frame%', '%engagement%', '%Whitfield%'. Show name and summary. Order by name. Filter by case_id = 17.

JOIN on suspect_info.id = suspect_reason.suspect_id and match case_id.

Hint
  • JOIN suspect_info and suspect_reason on si.id = sr.suspect_id and si.case_id = sr.case_id.
  • Filter summary with ILIKE '%revenge%' OR summary ILIKE '%frame%' OR summary ILIKE '%engagement%' OR summary ILIKE '%Whitfield%'.
Show the answer
SELECT si.name, sr.summary
FROM suspect_info si
JOIN suspect_reason sr ON sr.suspect_id = si.id AND sr.case_id = si.case_id
WHERE si.case_id = 17
  AND (sr.summary ILIKE '%revenge%' OR sr.summary ILIKE '%frame%'
       OR sr.summary ILIKE '%engagement%' OR sr.summary ILIKE '%Whitfield%')
ORDER BY si.name
Run it in Case 17 (question 4 of 5)

17. Whose motive mentions wealth, marriage, or the land?

Case 18: Endless Night · Hard · uses WHERE, ORDER BY, LIKE, JOIN

Use suspect_info and suspect_reason to find suspects whose motive summary mentions wealth, marriage, Gipsy's Acre, or money. Use ILIKE with patterns such as '%wealth%', '%marriage%', '%Gipsy%', '%money%'. Show name and summary. Order by name. Filter by case_id = 18.

JOIN on suspect_info.id = suspect_reason.suspect_id and match case_id.

Hint
  • JOIN suspect_info and suspect_reason on si.id = sr.suspect_id and si.case_id = sr.case_id.
  • Filter summary with ILIKE '%wealth%' OR summary ILIKE '%marriage%' OR summary ILIKE '%Gipsy%' OR summary ILIKE '%money%'.
Show the answer
SELECT si.name, sr.summary
FROM suspect_info si
JOIN suspect_reason sr ON sr.suspect_id = si.id AND sr.case_id = si.case_id
WHERE si.case_id = 18
  AND (sr.summary ILIKE '%wealth%' OR sr.summary ILIKE '%marriage%'
       OR sr.summary ILIKE '%Gipsy%' OR sr.summary ILIKE '%money%')
ORDER BY si.name
Run it in Case 18 (question 4 of 5)

More SQL exercises

Play the cases in order →