Find Customer Referee
Description
Write a SQL query to find customers not referred by customer with id = 2. Return customer names.
customer| Column | Type |
|---|---|
| id | INT |
| name | TEXT |
| referee_id | INT |
Examples
CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Alice', NULL), (2, 'Bob', NULL), (3, 'Charlie', 1), (4, 'Dave', 2);Alice
Bob
CharlieAlice and Bob have NULL referee_id values, so both are included. Charlie is referred by customer 1, so Charlie is also included. Dave is referred by customer 2, so Dave is the only row excluded.
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);Emma
John
SarahEmma 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.
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);Tom
Anna
Ben
KateAll 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