Home Cases Blog About

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.

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. Identify the Key Parties Involved

Case 6: Crooked House · Medium · uses WHERE, ORDER BY

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
Run it in Case 6 (question 1 of 5)

2. Meet the suspects

Case 16: A Murder is Announced · Easy · uses WHERE, ORDER BY

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
Run it in Case 16 (question 1 of 5)

3. Meet the cast

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

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
Run it in Case 18 (question 1 of 5)

4. Meet the suspects

Case 15: The A.B.C. Murders · Easy · uses WHERE, ORDER BY

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
Run it in Case 15 (question 1 of 5)

5. Identify the Key Parties Involved

Case 4: Murder in Mesopotamia · Medium · uses WHERE, DISTINCT

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
Run it in Case 4 (question 1 of 5)

6. Meet the suspects

Case 14: The Murder at the Vicarage · Easy · uses WHERE, ORDER BY

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
Run it in Case 14 (question 1 of 5)

7. List All Accusations

Case 7: And Then There Were None · Hard · uses WHERE, ORDER BY

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
Run it in Case 7 (question 1 of 5)

8. Who confessed to the murder?

Case 14: The Murder at the Vicarage · Easy · uses WHERE, ORDER BY

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
Run it in Case 14 (question 4 of 5)

9. Key events at Gipsy's Acre

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

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
Run it in Case 18 (question 2 of 5)

10. What is the timeline of alibis from earliest to latest?

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

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
Run it in Case 4 (question 2 of 5)

11. Where did each murder happen?

Case 15: The A.B.C. Murders · Easy · uses WHERE, ORDER BY, IS NULL

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
Run it in Case 15 (question 2 of 5)

12. Who are the victims?

Case 18: Endless Night · Hard · uses WHERE, ORDER BY, DISTINCT, IS NULL

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
Run it in Case 18 (question 3 of 5)

13. Meet the Suspects

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

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
Run it in Case 10 (question 1 of 5)

14. Which Round Had the Highest Bid, and Who Was the Dummy?

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

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
Run it in Case 11 (question 4 of 5)

15. Meet the people involved

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

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
Run it in Case 12 (question 1 of 5)

16. Meet the suspects and their relation to the victim

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

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
Run it in Case 13 (question 1 of 5)

17. Meet the villagers

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

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
Run it in Case 17 (question 1 of 5)

More SQL exercises

Play the cases in order →