As mentioned before, I’m tackling the LeetCode SQL challenge.
This week’s problem, however, is surprisingly simple, making me wonder if it’s worth a dedicated write-up.

Find the names of the customer that are not referred by the customer with id = 2.
Return the result table in any order.
The result format is in the following example.

Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Input: 
Customer table:
+----+------+------------+
| id | name | referee_id |
+----+------+------------+
| 1  | Will | null       |
| 2  | Jane | null       |
| 3  | Alex | 2          |
| 4  | Bill | null       |
| 5  | Zack | 1          |
| 6  | Mark | 2          |
+----+------+------------+
Output: 
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

Solution

To avoid repeating myself from the previous post (link above), I want to clarify that filtering for null values requires the IS NULL operator, not the equals sign (=).
The equals sign typically compares numeric values (integers, decimals, etc.).

1
2
3
4
# Write your MySQL query statement below
SELECT name
FROM Customer
WHERE referee_id IS null OR referee_id != 2

See you in the next post


Cover image by [ kirill_makes_pics] from Pixabay.