- in this interview question the bad query fails beacasse of null values in the races table.
- adding a where not field is null exludes the nulls and allows the function to work correctly
- using a subquery within the where clause based on a forieng key is like an inner join.
- using a negation of the above produces a filter of non winner.
- cheking null is done using IS not =
Query 1:
select * from races
| id | event | winner_id |
|----|----------------|-----------|
| 1 | 100 meter dash | 2 |
| 2 | 500 meter dash | 3 |
| 3 | cross-country | 2 |
| 4 | triathalon | (null) |
Query 2:
select * from runners
| id | name |
|----|-------------|
| 1 | John Doe |
| 2 | Jane Doe |
| 3 | Alice Jones |
| 4 | Bobby Louis |
| 5 | Lisa Romero |
Query 3:
SELECT *
FROM runners
WHERE id NOT IN (SELECT winner_id
FROM races
where not winner_id is null)
| id | name |
|----|-------------|
| 1 | John Doe |
| 4 | Bobby Louis |
| 5 | Lisa Romero |