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
ID 2WillJaneAlexBillZackMarkID 1Customer Referral Networkโœ“ Include (Green): Not referred by ID 2โœ— Exclude (Red): Referred by ID 2
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space needed for filtering

n
2n
โœ“ 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
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
25.4K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen