LeetCode 584. Find Customer Referee SQL Solution
Problem
LeetCode SQL Problem
- Find Customer Referee
Customer table
id | name | referee_id |
---|---|---|
1 | Will | null |
2 | Jane | null |
3 | Alex | 2 |
4 | Bill | null |
5 | Zack | 1 |
6 | Mark | 2 |
Solution
- MySQL
- TSQL
SELECT name
FROM customer
WHERE referee_id <> 2
OR referee_id IS NULL
SELECT name
FROM customer
WHERE referee_id <> 2
OR referee_id IS NULL
The above SQL query retrieves the names of customers whose referee ID is not equal to 2 or is NULL. Here is a breakdown of the different parts of the query:
-
SELECT name
: This specifies the column to be retrieved from the table, which is the customer name. -
FROM customer
: This specifies the table to be queried, which is the customer table. -
WHERE referee_id <> 2 OR referee_id IS NULL
: This is a conditional statement that filters the results based on certain criteria. In this case, it retrieves only those customers whose referee ID is not equal to 2 or is NULL. The<>
symbol means "not equal to", and theOR
operator combines two conditions together.
Overall, this query retrieves the names of customers NOT referred by the customer with id 2
.
Query Output
name |
---|
Will |
Jane |
Bill |
Zack |