SQL GROUP BY and HAVING Exercises
10 exercises with answers, easiest first.
GROUP BY turns many rows into one row per group, so you can count alibis per room or events per suspect. HAVING then filters those groups.
- 10 exercises, each from a real murder case
- 18% 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. How many suspects claimed each alibi location between 4:45 and 5:15 AM
Use the suspect_info table to list the alibi locations and the number of suspects who claimed to be at each between 4:45 and 5:15 AM.
filter by case_id = 3 in your query to focus only on the current investigation. Times are stored as HH:MM:SS, so 4:45 AM is '04:45:00'.
16% of players got this right on their first query (115 players, median 4 tries).
Hint
- Alibi times are in HH:MM:SS format. Ex. 4:45AM is '04:45:00'.
- Use the COUNT() function to tally the number of suspects per alibi location.
Show the answer
SELECT location, COUNT(name)
FROM suspect_info
WHERE case_id = 3
AND alibi_time BETWEEN '04:45:00' AND '05:15:00'
GROUP BY location
2. Who shared alibi locations?
Use the suspect_info table to group suspects by their alibi locations and list all names present at each location in alphabetical order, separated by a comma.
filter by case_id = 5 in your query to focus only on this investigation.
16% of players got this right on their first query (25 players, median 5 tries).
Hint
- Use STRING_AGG() to concatenate the names into a single comma-separated list.
Show the answer
SELECT
location,
STRING_AGG(name, ', ' ORDER BY name) AS suspects
FROM suspect_info
WHERE case_id = 5
GROUP BY location
3. Which family roles repeat?
Use the suspect_info table to list each role that appears more than once, along with how many people have it.
filter by case_id = 6 in your query to focus only on this investigation.
15% of players got this right on their first query (27 players, median 6 tries).
Show the answer
SELECT role, COUNT(*)
FROM suspect_info
WHERE case_id = 6
GROUP BY role
HAVING COUNT(*) > 1
4. Find the Jewel Thief
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
5. Find Who Was Mentioned Most
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
6. How Many Suspects vs Investigators?
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'
7. How Many Times Was Each Suspect the Dummy?
During the bridge game, each round had a player designated as the dummy (who could leave the table). Use the events table where event_type = 'bridge_round'. The suspect_name is who was dummy in that round. Count how many rounds each person was dummy. Show suspect_name and dummy_count. Order by dummy_count DESC, then by suspect_name.
filter by case_id = 11.
Hint
- Use GROUP BY suspect_name and COUNT(*) to count how many times each person appears in the bridge_round events.
- Filter with WHERE case_id = 11 and event_type = 'bridge_round'.
- Use ORDER BY dummy_count DESC, suspect_name.
Show the answer
SELECT suspect_name, COUNT(*) AS dummy_count
FROM events
WHERE case_id = 11 AND event_type = 'bridge_round'
GROUP BY suspect_name
ORDER BY dummy_count DESC, suspect_name
8. Who appears in the timeline of events?
The case has a sequence of events. List each person who appears in the events table and the time of their first recorded involvement. Show suspect_name and first_seen (the earliest event_timestamp for that person). Filter by case_id = 12. Order by first_seen.
filter by case_id = 12.
Hint
- Use GROUP BY suspect_name and MIN(event_timestamp) AS first_seen on the events table.
- Filter with WHERE case_id = 12, then ORDER BY first_seen.
Show the answer
SELECT suspect_name, MIN(event_timestamp) AS first_seen
FROM events
WHERE case_id = 12
GROUP BY suspect_name
ORDER BY first_seen
9. First and last appearance in the timeline
For each person who appears in the events table (case_id = 13), show their first and last recorded time: suspect_name, first_seen (MIN(event_timestamp)), last_seen (MAX(event_timestamp)). Order by first_seen.
filter by case_id = 13.
Hint
- Use GROUP BY suspect_name with MIN(event_timestamp) AS first_seen and MAX(event_timestamp) AS last_seen.
- Filter with WHERE case_id = 13, then ORDER BY first_seen.
Show the answer
SELECT suspect_name, MIN(event_timestamp) AS first_seen, MAX(event_timestamp) AS last_seen
FROM events
WHERE case_id = 13
GROUP BY suspect_name
ORDER BY first_seen
10. Who appears in the most events?
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