Find Customer Referee
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Description
You have a Customer table where each customer may have been referred by another customer (stored as referee_id). Write a SQL query to find the names of customers who were NOT referred by the customer with id = 2. Return the result in any order.
Database Schema
Customer
| Column Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| name | VARCHAR | Customer name |
| referee_id | INT | ID of customer who referred them (nullable) |
Example
Customer
| id | name | referee_id |
|---|---|---|
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
Output
| name |
|---|
| Will |
| Jane |
| Bill |
| Zack |
Explanation:
Alex and Mark were referred by customer 2. All others (including those with NULL referee_id) are returned.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
SQL Editor
Loading...
Customer
| id | name | referee_id |
|---|---|---|
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
Output
| name |
|---|
| Will |
| Jane |
| Bill |
| Zack |