File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Problem Description:
3+
4+ +-------------+---------+
5+ | Column Name | Type |
6+ +-------------+---------+
7+ | id | int |
8+ | name | varchar |
9+ | referee_id | int |
10+ +-------------+---------+
11+ id is the primary key column for this table.
12+ Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
13+
14+ Write an SQL query to report the names of the customer that are not referred by the customer with id = 2.
15+
16+ Return the result table in any order.
17+
18+ The query result format is in the following example.
19+
20+ Example 1:
21+
22+ Input:
23+ Customer table:
24+ +----+------+------------+
25+ | id | name | referee_id |
26+ +----+------+------------+
27+ | 1 | Will | null |
28+ | 2 | Jane | null |
29+ | 3 | Alex | 2 |
30+ | 4 | Bill | null |
31+ | 5 | Zack | 1 |
32+ | 6 | Mark | 2 |
33+ +----+------+------------+
34+ Output:
35+ +------+
36+ | name |
37+ +------+
38+ | Will |
39+ | Jane |
40+ | Bill |
41+ | Zack |
42+ +------+
43+ */
44+
45+ # Write your MySQL query statement below
46+
47+ SELECT name
48+ FROM Customer
49+
50+ # COALESCE is used to replace NULL values with zero before checking whether it is equal to 2 or not.
51+ WHERE COALESCE(referee_id,0) <> 2;
52+
53+ # Or you can use IFNULL to determine, which is faster
54+ WHERE IFNULL(referee_id,0) <> 2;
You can’t perform that action at this time.
0 commit comments