Description
Write a SQL query to find customers not referred by customer with id = 2. Return customer names.
Table:
customer| Column | Type |
|---|---|
| id | INT |
| name | TEXT |
| referee_id | INT |
Examples
Input:
CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Alice', NULL), (2, 'Bob', 2), (3, 'Charlie', 1);Output:
Alice
CharlieExplanation:
Alice has NULL referee_id (not referred by 2). Bob has referee_id=2 (referred by customer 2), so excluded. Charlie has referee_id=1 (not referred by 2).
Input:
CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Emma', 3), (2, 'John', NULL), (3, 'Sarah', NULL), (4, 'Mike', 2), (5, 'Lisa', 2);Output:
Emma
John
SarahExplanation:
Emma is referred by customer 3 (not 2), John has NULL referee_id (not referred by 2), and Sarah has NULL referee_id (not referred by 2). Mike and Lisa are both referred by customer 2, so they are excluded from the results.
Input:
CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Tom', NULL), (2, 'Anna', 1), (3, 'Ben', NULL), (4, 'Kate', NULL);Output:
Tom
Anna
Ben
KateExplanation:
All customers are returned because none are referred by customer with id = 2. Tom, Ben, and Kate all have NULL referee_id values, while Anna is referred by customer 1. Since no one is referred by customer 2, all customers meet the criteria.
Constraints
- •
Handle NULL referee_id