↓Skip to main content
  1. LeetCode/

LeetCode 584: Find Customer Referee

·1 min· ·
LeetCode Easy Database Sql
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

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 = NULL nor != 2 selects 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.

Related

LeetCode 1757: Recyclable and Low Fat Products
·1 min
LeetCode Easy Database Sql
LeetCode 1757 note: both conditions must hold, so filter with WHERE … AND …
LeetCode 197: Rising Temperature
·1 min
LeetCode Easy Database
LeetCode 197 note: self-join Weather on a one-day date difference with today warmer; compare dates with DATEDIFF, never ids
LeetCode 595: Big Countries
·1 min
LeetCode Easy Database
LeetCode 595 note: filter with WHERE and OR for an area of at least 3 million or a population of at least 25 million
LeetCode 596: Classes With at Least 5 Students
·1 min
LeetCode Easy Database
LeetCode 596 note: group by class with HAVING COUNT(DISTINCT student) >= 5; older test data had duplicate rows, so count distinct students
LeetCode 620: Not Boring Movies
·1 min
LeetCode Easy Database
LeetCode 620 note: odd id and description not ‘boring’, ordered by rating from high to low
LeetCode 175: Combine Two Tables
·1 min
LeetCode Easy Database
LeetCode 175 note: people without an address must still appear, so LEFT JOIN, leaving city and state NULL when unmatched