Skip to main content

LeetCode 584. Find Customer Referee SQL Solution

Problem

LeetCode SQL Problem

  1. Find Customer Referee

Customer table

idnamereferee_id
1Willnull
2Janenull
3Alex2
4Billnull
5Zack1
6Mark2

Solution

Find Customer Referee
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 the OR 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