Home Cases Blog About

SQL Subquery and CTE Exercises

5 exercises with answers, easiest first.

Some questions need two steps. A subquery or a CTE (a WITH block) lets you answer the first step, give it a name and query the result.

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. Find the Jewel Thief

Case 8: Death on the Nile · Hard · uses WHERE, ORDER BY, LIMIT, IN, COUNT, GROUP BY, CTE

Find suspects with 'theft' or 'seen with jewels' events. From that group, identify who has the most total events. Show suspect_name and total_event_count.

filter by case_id = 8 in your query to focus only on the current investigation. Use a subquery with WHERE IN, then GROUP BY with ORDER BY ... LIMIT.

Show the answer
SELECT
  suspect_name,
  COUNT(*) AS total_event_count
FROM events
WHERE case_id = 8
  AND suspect_name IN (
    SELECT suspect_name
    FROM events
    WHERE case_id = 8
      AND event_type IN ('theft', 'seen with jewels')
  )
GROUP BY suspect_name
ORDER BY total_event_count DESC
LIMIT 1
Run it in Case 8 (question 4 of 5)

2. 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)

3. Find Who Was Mentioned Most

Case 9: Five Little Pigs · Hard · uses WHERE, ORDER BY, DISTINCT, LIKE, COUNT, GROUP BY

Find which suspect was mentioned by the most other suspects in testimonies. Show suspect_name and times_mentioned. Order by times mentioned descending.

filter by case_id = 9 in your query. Use correlated subqueries with COUNT(DISTINCT).

Show the answer
SELECT
  si.name AS suspect_name,
  (SELECT COUNT(DISTINCT e.suspect_name)
   FROM events e
   WHERE e.case_id = 9
     AND e.event_type = 'testimony'
     AND e.details ILIKE '%' || si.name || '%'
     AND e.suspect_name != si.name
  ) AS times_mentioned
FROM suspect_info si
WHERE si.case_id = 9
GROUP BY si.name
ORDER BY times_mentioned DESC, si.name
Run it in Case 9 (question 4 of 5)

4. Who Was First at Each Location?

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

Use the suspect_info table to find, for each location, the suspect who arrived earliest (lowest alibi_time). Show location and name. Order by location.

filter by case_id = 10.

Hint
  • Use a subquery to find the minimum alibi_time per location, then match rows where alibi_time equals that minimum.
Show the answer
SELECT s.location, s.name
FROM suspect_info s
WHERE s.case_id = 10
  AND s.alibi_time = (
    SELECT MIN(s2.alibi_time)
    FROM suspect_info s2
    WHERE s2.case_id = 10 AND s2.location = s.location
  )
ORDER BY s.location
Run it in Case 10 (question 4 of 5)

5. How Many Suspects vs Investigators?

Case 11: Cards on the Table · Medium · uses WHERE, IN, COUNT, UNION

Shaitana invited four guests he suspected of past murders, plus three crime investigators. Use the suspect_info and suspect_reason tables to count each group. Guests with a suspect_reason row where reason_type = 'crime' are suspects; the rest are investigators. Show role_category and count.

filter by case_id = 11 in your query.

Hint
  • Use UNION ALL to combine two queries: one for Investigators, one for Suspects.
  • Investigators = rows in suspect_info where id is NOT IN the suspect_id values from suspect_reason where reason_type = 'crime'.
  • Suspects = count rows in suspect_reason where reason_type = 'crime' and case_id = 11.
Show the answer
SELECT 'Investigator' AS role_category, COUNT(*) AS count
FROM suspect_info si
WHERE si.case_id = 11
  AND si.id NOT IN (
    SELECT suspect_id FROM suspect_reason
    WHERE case_id = 11 AND reason_type = 'crime'
  )
UNION ALL
SELECT 'Suspect', COUNT(*)
FROM suspect_reason
WHERE case_id = 11 AND reason_type = 'crime'
Run it in Case 11 (question 1 of 5)

More SQL exercises

Play the cases in order →