Meta Data#
Difficulty: easy First Attempt: 2025-12-23
- Submissions: Accepted (first try)
Approach#
We want customers not referred by customer 2, which includes those with no referrer (referee_id is NULL).
SQL comparisons have three outcomes, and NULL != 2 is unknown, not true, while WHERE keeps only true rows (SQL Template 2). Writing only referee_id != 2 therefore drops the NULL rows; add IS NULL explicitly.
My Solution#
SELECT name
FROM Customer
WHERE referee_id IS NULL OR referee_id != 2;
WHERE COALESCE(referee_id, 0) != 2 also works, replacing NULL with a value that cannot be 2.
Complexity#
One table scan, O(n).
Mistakes to Avoid#
- Test for NULL with
IS NULL; neither= NULLnor!= 2selects NULL rows.
Interview Explanation#
Customers not referred by 2 include those with no referrer, but NULL != 2 is unknown in SQL and WHERE drops it, so the condition is referee_id IS NULL OR referee_id != 2.
