Add SQL query to find customers without a referee #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Title Format: 584.Find Customer Referee.sql
💡 Intuition
The goal is to list the names of all customers except for those whose referee ID is exactly 2. This requires filtering the Customer table based on the referee_id column. We must account for two conditions where a customer should be included in the result:
The referee_id is a number other than 2.
The referee_id is NULL. In SQL, comparing a column to NULL using standard comparison operators (!=, =, etc.) always results in UNKNOWN, which is treated as false in a WHERE clause. Therefore, we must explicitly check for IS NULL.
✍️ Approach
The solution uses a single WHERE clause with an OR condition to filter the rows correctly:
Selection: Select the name column from the Customer table.
Filtering: The WHERE clause includes customers who satisfy either of the following conditions:
referee_id IS NULL: This correctly includes customers who do not have a referee assigned.
referee_id != 2: This correctly includes customers whose referee ID is any value other than 2 (e.g., 1, 3, etc.).
By combining these two conditions with OR, we ensure that the list includes all customers who are either unreferred or referred by anyone other than customer '2'.
Code Solution (SQL)
SQL
SELECT name
FROM Customer
WHERE referee_id IS NULL OR referee_id != 2;
🔗 Related Issues
By submitting this PR, I confirm that:
[x] This is my original work not totally AI generated
[x] I have tested the solution thoroughly on leetcode
[x] I have maintained proper PR description format
[x] This is a meaningful contribution, not spam