SQL LIKE, BETWEEN and IN Exercises
27 exercises with answers, easiest first.
Real data is messy, so exact matches are rarely enough. LIKE finds text that contains a word. BETWEEN covers a range of times. IN checks a list of values.
- 27 exercises, each from a real murder case
- 34% 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. Who was paid to stage the fake hold-up?
The dead man had been paid to stage a fake robbery. List name and role from suspect_info where case_id = 16 and role contains the word 'stage' (use ILIKE '%stage%').
filter by case_id = 16.
85% of players got this right on their first query (33 players, median 1 try).
Hint
- Use WHERE case_id = 16 and role ILIKE '%stage%'.
- Select name and role.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 16 AND role ILIKE '%stage%'
2. Who stood to gain from the third victim's death?
Sir Carmichael Clarke was the third victim. List name from suspect_info where case_id = 15 and role contains the word 'brother' (use ILIKE '%brother%').
filter by case_id = 15.
82% of players got this right on their first query (39 players, median 1 try).
Hint
- Use WHERE case_id = 15 and role ILIKE '%brother%'.
- Select name only.
Show the answer
SELECT name
FROM suspect_info
WHERE case_id = 15 AND role ILIKE '%brother%'
3. Who was at Little Paddocks that night?
The murder was announced at Little Paddocks. List each person whose location is Little Paddocks (where location contains 'Little Paddocks'). Show name, location, and alibi_time. Use suspect_info with case_id = 16. Order by alibi_time.
use ILIKE '%Little Paddocks%' to match the location.
79% of players got this right on their first query (34 players, median 1 try).
Hint
- Use WHERE case_id = 16 and location ILIKE '%Little Paddocks%'.
- Select name, location, alibi_time. ORDER BY alibi_time.
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 16 AND location ILIKE '%Little Paddocks%'
ORDER BY alibi_time
4. Who stood to inherit if Miss Blacklock died?
List name from suspect_info where case_id = 16 and role contains the word 'inherit' (use ILIKE '%inherit%'). Order by name.
filter by case_id = 16.
67% of players got this right on their first query (33 players, median 1 try).
Hint
- Use WHERE case_id = 16 and role ILIKE '%inherit%'.
- Select name only. ORDER BY name.
Show the answer
SELECT name
FROM suspect_info
WHERE case_id = 16 AND role ILIKE '%inherit%'
ORDER BY name
5. Who was wrongly accused?
The police initially suspected someone who was framed. List name and role from suspect_info where case_id = 15 and role contains the word 'framed' (use ILIKE '%framed%').
filter by case_id = 15.
65% of players got this right on their first query (40 players, median 1 try).
Hint
- Use WHERE case_id = 15 and role ILIKE '%framed%'.
- Select name and role.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 15 AND role ILIKE '%framed%'
6. Who never claimed to be at Pixy Cove?
Use the suspect_info table to list each guest's name whose alibi location is not 'Pixy Cove'.
filter by case_id = 5 in your query to focus only on this investigation.
64% of players got this right on their first query (25 players, median 1 try).
Show the answer
SELECT name
FROM suspect_info
WHERE case_id = 5
AND location NOT LIKE '%Pixy Cove%'
7. Who is the artist?
The vicarage murder involves an artist. List name and role from suspect_info where case_id = 14 and role contains the word 'artist' (use ILIKE '%artist%').
filter by case_id = 14.
54% of players got this right on their first query (41 players, median 1 try).
Hint
- Use WHERE case_id = 14 and role ILIKE '%artist%'.
- Select name and role.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 14 AND role ILIKE '%artist%'
8. Who was not in the bedroom when Emily died?
Use the suspect_info table to list each suspect's name, alibi location and alibi time if they were not in the bedroom at the time of Emily's death.
filter by case_id = 3 in your query to focus only on the current investigation.
51% of players got this right on their first query (101 players, median 1 try).
Hint
- Use a negation operator (NOT IN or !=) to exclude the Bedroom.
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 3
AND location NOT IN ('Bedroom')
9. Who was in the bedroom between 4:45 to 5:15 AM?
Use the suspect_info table, list each suspect's name, alibi location and alibi time where alibi location is 'Bedroom' and alibi time is between 4:45 AM to 5:15 AM, ordered by alibi time.
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'.
48% of players got this right on their first query (102 players, median 2 tries).
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 3
AND alibi_time BETWEEN '04:45:00' AND '05:15:00'
AND location = 'Bedroom'
ORDER BY alibi_time
10. Who was at the vicarage?
List each person whose alibi is at the vicarage (where location contains 'Vicarage'). Show name, location, and alibi_time. Use suspect_info with case_id = 14. Order by alibi_time.
use ILIKE '%Vicarage%' to match locations that contain the word Vicarage.
34% of players got this right on their first query (44 players, median 2 tries).
Hint
- Use WHERE case_id = 14 and location ILIKE '%Vicarage%'.
- Select name, location, alibi_time. ORDER BY alibi_time.
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 14 AND location ILIKE '%Vicarage%'
ORDER BY alibi_time
11. What were the first names of every Leonides family member?
Use the suspect_info table to extract the first name of each suspect whose last name is 'Leonides' and order them in alphabetical order.
filter by case_id = 6 in your query to focus only on this investigation.
30% of players got this right on their first query (23 players, median 4 tries).
Hint
- Use the SPLIT_PART() function to separate the first name from the full name.
Show the answer
SELECT
split_part(name, ' ', 1) AS first_name
FROM suspect_info
WHERE case_id = 6
AND name LIKE '% Leonides'
ORDER BY first_name
12. Who Was in Compartment 4?
Two suspects were reportedly seen in Compartment 4 between 01:00 and 02:00 AM. Use the suspect_info table to list every suspect's name who claimed to be in 'Compartment 4' during that time.
filter by case_id = 1 in your query to focus only on the current investigation. Times are stored as HH:MM:SS, so 01:00 AM is '01:00:00'.
21% of players got this right on their first query (384 players, median 4 tries).
Hint
- Use location = 'Compartment 4'
- Use BETWEEN '01:00:00' AND '02:00:00' to include the whole hour.
Show the answer
SELECT name
FROM suspect_info
WHERE case_id = 1
AND location = 'Compartment 4'
AND alibi_time BETWEEN '01:00:00' AND '02:00:00'
13. Who was in the Drawing Room or the Garden?
Use the suspect_info table to list each suspect's name, location and alibi_time for those whose alibi is either 'Drawing Room' or 'Garden'.
filter by case_id = 2 in your query to focus only on the current investigation.
19% of players got this right on their first query (195 players, median 4 tries).
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 2
AND location IN ('Drawing Room', 'Garden')
14. 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
15. Which "Sheppard" family members were in the Study?
Query the suspect_info table to return only the name of each Sheppard family member whose location is 'Study'.
filter by case_id = 2 in your query to focus only on the current investigation.
15% of players got this right on their first query (178 players, median 4 tries).
Hint
- Use the LIKE operator to focus on Sheppard family members. ex. LIKE '%Sheppard%'
Show the answer
SELECT name
FROM suspect_info
WHERE case_id = 2
AND name LIKE '%Sheppard%'
AND location = 'Study'
16. Determine the Sequence of Deaths
Use the events table to list each victim's name and assign a death order (numbered 1, 2, 3, etc.) based on the event_timestamp, ordered chronologically.
filter by case_id = 7 in your query to focus only on the current investigation. Use ROW_NUMBER() window function to assign the sequence.
Hint
- Use ROW_NUMBER() OVER (ORDER BY event_timestamp) to assign sequential numbers.
- Filter for death events using event_type IN ('poisoned', 'murder').
- Order the final results by death_order to see the sequence.
Show the answer
SELECT
suspect_name AS name,
ROW_NUMBER() OVER (ORDER BY event_timestamp) AS death_order
FROM events
WHERE case_id = 7
AND event_type IN ('poisoned', 'murder')
ORDER BY death_order
17. Cross-Reference Accusations with Death Events
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')
18. 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
19. 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
20. Build the Testimony Chain
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
21. 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
22. Who Had Alibis at Key Locations?
Use the suspect_info table to list suspects whose location is either 'Savoy Hotel' or 'Edgware House'. Show name, location, and alibi_time, ordered by alibi_time.
filter by case_id = 10.
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 10
AND location IN ('Savoy Hotel', 'Edgware House')
ORDER BY alibi_time
23. 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'
24. Whose motive was tied to the victim's past?
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
25. Sequence of "accidents"
The deaths looked like accidents. Use the events table to list each victim (from details), the event_type (e.g. poisoned, hit-and-run, pushed), and event_timestamp. Add a column death_order (1, 2, 3, …) using ROW_NUMBER() over event_timestamp. Filter by case_id = 17 and only fatal event types. Order by event_timestamp.
filter by case_id = 17 and event_type IN ('poisoned','hit-and-run','pushed','murder').
Hint
- Use details AS victim_name, event_type, event_timestamp.
- Use ROW_NUMBER() OVER (ORDER BY event_timestamp) AS death_order.
- Filter with WHERE case_id = 17 and event_type IN ('poisoned','hit-and-run','pushed','murder').
Show the answer
SELECT details AS victim_name, event_type, event_timestamp,
ROW_NUMBER() OVER (ORDER BY event_timestamp) AS death_order
FROM events
WHERE case_id = 17
AND event_type IN ('poisoned', 'hit-and-run', 'pushed', 'murder')
ORDER BY event_timestamp
26. Whose motive points to revenge or framing?
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
27. Whose motive mentions wealth, marriage, or the land?
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