easySELECTWHERENULL handling
Find Customer Referee
Find customers who are not referred by customer id 2.
Start practicePrompt
Find Customer Referee
Prompt
Given the customer table below, write a query that returns the name of every customer who was not referred by customer with `referee_id` = 2.
- Output a single column:
name. - A customer with no referee (
referee_id IS NULL) is not referred by 2 and must be included. - The order of the result does not matter.
Example
| id | name | referee_id |
|---|---|---|
| 1 | Alice | NULL |
| 2 | Bob | 1 |
| 3 | Carol | 2 |
| 4 | Dave | 3 |
Expected result:
| name |
|---|
| Alice |
| Bob |
| Dave |
Constraints
customer.idis the primary key.customer.referee_idis nullable.
Tips
referee_id <> 2excludes rows wherereferee_id IS NULL(a NULL comparison is never true) — you needreferee_id <> 2 OR referee_id IS NULL.COALESCE(referee_id, -1) <> 2is the classic one-liner alternative.
Expected concepts
- WHERE
- NULL comparison
- COALESCE / IS NULL
Schema
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
referee_id INTEGER -- NULL allowed
);
Sample data
INSERT INTO customer (id, name, referee_id) VALUES
(1, 'Alice', NULL),
(2, 'Bob', 1),
(3, 'Carol', 2),
(4, 'Dave', 3),
(5, 'Eve', 2);
Additional hidden fixtures are applied during validation to test edge cases.