SQL ORDER BY and DISTINCT Exercises
17 exercises with answers, easiest first.
Once you can filter rows, the next step is shaping the result. ORDER BY turns a pile of events into a timeline. DISTINCT removes repeats. LIMIT keeps the top few.
- 17 exercises, each from a real murder case
- 72% 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. Identify the Key Parties Involved
Use the suspect_info table to list the names and relations of all the key parties involved in this investigation and order the results alphabetically.
filter by case_id = 6 in your query to focus only on the current investigation.
90% of players got this right on their first query (30 players, median 1 try).
Show the answer
SELECT name, relation
FROM suspect_info
WHERE case_id = 6
ORDER BY name
2. Meet the suspects
List everyone connected to the case: name and role. Use the suspect_info table with case_id = 16. Order by name.
filter by case_id = 16 in your query.
89% of players got this right on their first query (35 players, median 1 try).
Hint
- Use SELECT name, role from suspect_info with WHERE case_id = 16 and ORDER BY name.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 16
ORDER BY name
3. Meet the cast
List everyone connected to the case: name and role. Use the suspect_info table with case_id = 18. Order by name.
filter by case_id = 18 in your query.
88% of players got this right on their first query (24 players, median 1 try).
Hint
- Use SELECT name, role from suspect_info with WHERE case_id = 18.
- Order by name.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 18
ORDER BY name
4. Meet the suspects
List everyone connected to the case: name and role. Use the suspect_info table with case_id = 15. Order by name.
filter by case_id = 15 in your query.
80% of players got this right on their first query (44 players, median 1 try).
Hint
- Use SELECT name, role from suspect_info with WHERE case_id = 15 and ORDER BY name.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 15
ORDER BY name
5. Identify the Key Parties Involved
Use the suspect_info table to list the names and roles of all the key parties involved in this investigation.
filter by case_id = 4 in your query to focus only on the current investigation.
79% of players got this right on their first query (91 players, median 1 try).
Show the answer
SELECT DISTINCT name, role
FROM suspect_info
WHERE case_id = 4
6. Meet the suspects
List everyone connected to the case: name and role. Use the suspect_info table with case_id = 14. Order by name.
filter by case_id = 14 in your query.
75% of players got this right on their first query (48 players, median 1 try).
Hint
- Use SELECT name, role from suspect_info with WHERE case_id = 14 and ORDER BY name.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 14
ORDER BY name
7. List All Accusations
Use the suspect_info table to list the name and role (their accusation/crime) of all ten guests on Soldier Island, ordered alphabetically by name.
filter by case_id = 7 in your query to focus only on the current investigation.
74% of players got this right on their first query (23 players, median 1 try).
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 7
ORDER BY name
8. Who confessed to the murder?
Both Lawrence Redding and Anne Protheroe confessed. List suspect_name from the events table where case_id = 14 and event_type = 'confession'. Order by suspect_name.
filter by case_id = 14 and event_type = 'confession'.
66% of players got this right on their first query (38 players, median 1 try).
Hint
- Use SELECT suspect_name FROM events with WHERE case_id = 14 AND event_type = 'confession'.
- ORDER BY suspect_name.
Show the answer
SELECT suspect_name
FROM events
WHERE case_id = 14 AND event_type = 'confession'
ORDER BY suspect_name
9. Key events at Gipsy's Acre
List the main events: event_type, event_timestamp, and details (e.g. victim or description). Use the events table with case_id = 18. Order by event_timestamp.
filter by case_id = 18 in your query.
64% of players got this right on their first query (25 players, median 1 try).
Hint
- Use SELECT event_type, event_timestamp, details from events.
- Filter with WHERE case_id = 18 and ORDER BY event_timestamp.
Show the answer
SELECT event_type, event_timestamp, details
FROM events
WHERE case_id = 18
ORDER BY event_timestamp
10. What is the timeline of alibis from earliest to latest?
Use the suspect_info table to list each suspect's name, alibi location and alibi time, ordered from the earliest to latest.
filter by case_id = 4 in your query to focus only on the current investigation.
61% of players got this right on their first query (84 players, median 1 try).
Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 4
ORDER BY alibi_time
11. Where did each murder happen?
The killer followed an A to B to C pattern by town. List each victim's name and the location (town) where they were killed. Use suspect_info with case_id = 15 and only rows where location is not null. Order by location.
use WHERE location IS NOT NULL to get only victims with a murder location.
59% of players got this right on their first query (41 players, median 1 try).
Hint
- Use WHERE case_id = 15 and location IS NOT NULL.
- Select name, location. ORDER BY location.
Show the answer
SELECT name, location
FROM suspect_info
WHERE case_id = 15 AND location IS NOT NULL
ORDER BY location
12. Who are the victims?
From the events table, list the distinct victim names stored in the details column for fatal events (event_type = 'murder'). Show details as victim_name. Order by victim_name. Filter by case_id = 18.
use SELECT DISTINCT details AS victim_name with WHERE event_type = 'murder' and details IS NOT NULL.
45% of players got this right on their first query (20 players, median 2 tries).
Hint
- Use SELECT DISTINCT details AS victim_name from events.
- Filter with WHERE case_id = 18 AND event_type = 'murder' AND details IS NOT NULL.
- ORDER BY victim_name.
Show the answer
SELECT DISTINCT details AS victim_name
FROM events
WHERE case_id = 18
AND event_type = 'murder'
AND details IS NOT NULL
ORDER BY victim_name
13. Meet the Suspects
Use the suspect_info table to list the names and roles of everyone involved in the Lord Edgware case. Order by role alphabetically.
filter by case_id = 10 in your query.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 10
ORDER BY role
14. Which Round Had the Highest Bid, and Who Was the Dummy?
During the bridge game, a dramatic bid was placed in one round to focus everyone's attention on the game, allowing the dummy to slip away. Use the events table where event_type = 'bridge_round'. The details column stores each round as round|bid (e.g. 1|3 means round 1, bid level 3). The suspect_name is who was dummy in that round. Find the round with the highest bid and show the round number and dummy's name.
filter by case_id = 11. Use SPLIT_PART to split details on the pipe symbol |.
Hint
- The details column stores round and bid separated by a pipe symbol |. Example: 1|3 = round 1, bid 3; 2|5 = round 2, bid 5.
- Use SPLIT_PART(details, '|', 1) to get the round number (first part).
- Use SPLIT_PART(details, '|', 2) to get the bid level (second part).
- Cast the bid to integer for ordering: CAST(SPLIT_PART(details, '|', 2) AS INT).
- Order by the bid descending and use LIMIT 1 to get only the round with the highest bid.
- Select round_number (from SPLIT_PART) and suspect_name (as dummy_name).
Show the answer
SELECT
SPLIT_PART(e.details, '|', 1) AS round_number,
e.suspect_name AS dummy_name
FROM events e
WHERE e.case_id = 11 AND e.event_type = 'bridge_round'
ORDER BY CAST(SPLIT_PART(e.details, '|', 2) AS INT) DESC
LIMIT 1
15. Meet the people involved
List everyone connected to the case: name and role. Use the suspect_info table and filter by case_id = 12. Order by name.
filter by case_id = 12 in your query.
Hint
- Use SELECT name, role from suspect_info with WHERE case_id = 12 and ORDER BY name.
Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 12
ORDER BY name
16. Meet the suspects and their relation to the victim
List everyone connected to the case: name, role, and relation to the victim (Mrs. Boynton). Use the suspect_info table with case_id = 13. Order by relation, then name.
filter by case_id = 13 in your query.
Hint
- Use SELECT name, role, relation from suspect_info with WHERE case_id = 13 and ORDER BY relation, name.
Show the answer
SELECT name, role, relation
FROM suspect_info
WHERE case_id = 13
ORDER BY relation, name
17. Meet the villagers
Use the suspect_info table to list every villager's name, role, and location (where they live or work) for this case. Order by location (NULLs last), then by name.
filter by case_id = 17 in your query.
Hint
- Use SELECT name, role, location from suspect_info with WHERE case_id = 17.
- Order by location NULLS LAST, then name.
Show the answer
SELECT name, role, location
FROM suspect_info
WHERE case_id = 17
ORDER BY location NULLS LAST, name