Find Customer Referee - Problem
Imagine you're analyzing a customer database for a company's referral program. You have a table of customers where each customer might have been referred by another customer.
Goal: Find the names of customers who were NOT referred by customer with ID 2 specifically.
This includes:
- Customers referred by any other customer (ID โ 2)
- Customers who were not referred by anyone (NULL referee_id)
Customer Table Schema:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| referee_id | int |
+-------------+---------+The referee_id column contains the ID of the customer who made the referral, or NULL if no one referred them.
Input & Output
example_1.sql โ Basic Example
$
Input:
Customer table:
+----+------+------------+
| 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 |
+------+
๐ก Note:
Will, Jane, and Bill were not referred by anyone (null). Zack was referred by customer 1 (not 2). Alex and Mark were referred by customer 2, so they are excluded.
example_2.sql โ All Null References
$
Input:
Customer table:
+----+------+------------+
| id | name | referee_id |
+----+------+------------+
| 1 | John | null |
| 2 | Mary | null |
| 3 | Bob | null |
+----+------+------------+
โบ
Output:
+------+
| name |
+------+
| John |
| Mary |
| Bob |
+------+
๐ก Note:
All customers have null referee_id, so none were referred by customer 2. All names are included in the result.
example_3.sql โ Mixed References
$
Input:
Customer table:
+----+-------+------------+
| id | name | referee_id |
+----+-------+------------+
| 1 | Alice | 3 |
| 2 | Bob | 2 |
| 3 | Carol | 1 |
| 4 | Dave | 2 |
+----+-------+------------+
โบ
Output:
+-------+
| name |
+-------+
| Alice |
| Carol |
+-------+
๐ก Note:
Alice was referred by customer 3, Carol by customer 1. Bob and Dave were both referred by customer 2, so they are excluded.
Visualization
Tap to expand
Understanding the Visualization
1
Map the Network
Visualize all customers and their referral relationships
2
Identify Target
Mark customer ID 2 as the referrer to exclude
3
Filter Results
Select customers not referred by ID 2 or with no referrer
Key Takeaway
๐ฏ Key Insight: Use `WHERE referee_id != 2 OR referee_id IS NULL` to filter customers, remembering that NULL values require special handling in SQL comparisons.
Time & Space Complexity
Time Complexity
O(n)
Single table scan through all customers
โ Linear Growth
Space Complexity
O(1)
No additional space needed for filtering
โ Linear Space
Constraints
- 1 โค id โค 1000
- 1 โค name.length โค 50
- referee_id can be null or reference another customer's id
- referee_id will not reference a non-existent customer
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code