Home Cases Blog About

SQL SELECT and WHERE Exercises

7 exercises with answers, easiest first.

Every query starts here. You name the columns you want, name the table and keep only the rows that matter. These exercises use one table at a time.

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 3: The Mysterious Affair at Styles · Easy · uses WHERE

Use the suspect_info table to list the names and roles of all the key parties involved in this investigation.

filter by case_id = 3 in your query to focus only on the current investigation.

81% of players got this right on their first query (124 players, median 1 try).

Show the answer
SELECT name, role 
FROM suspect_info 
WHERE case_id = 3
Run it in Case 3 (question 1 of 5)

2. Identify the Key Parties Involved

Case 5: Evil Under the Sun · Medium · uses WHERE

Use the suspect_info table to list the names and relations of all the key parties involved in this investigation.

filter by case_id = 5 in your query to focus only on the current investigation.

81% of players got this right on their first query (31 players, median 1 try).

Show the answer
SELECT name, relation 
FROM suspect_info 
WHERE case_id = 5
Run it in Case 5 (question 1 of 5)

3. Who Had a Motive?

Case 1: Murder on the Orient Express · Easy · uses WHERE

Samuel Ratchett (previously known as Cassetti) was a criminal. Use the suspect_info table to list every suspect's name and relation to find out who had a motive.

filter by case_id = 1 in your query to focus only on the current investigation.

66% of players got this right on their first query (360 players, median 1 try).

Show the answer
SELECT name, relation
FROM suspect_info
WHERE case_id = 1
Run it in Case 1 (question 4 of 5)

4. Identify the Key Parties Involved

Case 2: The Murder of Roger Ackroyd · Easy · uses WHERE

Use the suspect_info table to list the names and roles of all the key parties involved in this investigation.

filter by case_id = 2 in your query to focus only on the current investigation.

64% of players got this right on their first query (208 players, median 1 try).

Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 2
Run it in Case 2 (question 1 of 5)

5. When and where was everyone?

Case 1: Murder on the Orient Express · Easy · uses WHERE

Each passenger gave an alibi, a time and place where they claimed to be during the night. Use the suspect_info table to list every suspect's name, alibi location and time to begin checking their stories.

filter by case_id = 1 in your query to focus only on the current investigation.

44% of players got this right on their first query (435 players, median 2 tries).

Show the answer
SELECT name, location, alibi_time
FROM suspect_info
WHERE case_id = 1
Run it in Case 1 (question 2 of 5)

6. Identify the Passengers

Case 1: Murder on the Orient Express · Easy · uses WHERE

Use the suspect_info table to list the names and roles of all the passengers on the train.

filter by case_id = 1 in your query to focus only on the current investigation.

30% of players got this right on their first query (505 players, median 2 tries).

Show the answer
SELECT name, role
FROM suspect_info
WHERE case_id = 1
Run it in Case 1 (question 1 of 5)

7. Who last saw Roger Ackroyd alive?

Case 2: The Murder of Roger Ackroyd · Easy · uses WHERE

Query the events table to identify the names of those who last saw Roger Ackroyd alive.

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 (164 players, median 3 tries).

Hint
  • Look for event_type = 'seen alive' to find out who last saw Roger Ackroyd alive.
Show the answer
SELECT suspect_name
FROM events
WHERE case_id = 2
  AND event_type = 'seen alive'
Run it in Case 2 (question 4 of 5)

More SQL exercises

Play the cases in order →