File tree
Expand file treeCollapse file tree1 file changed
+53
-0
lines changed Expand file treeCollapse file tree1 file changed
+53
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +-- Problem: |
| 2 | + |
| 3 | +/* |
| 4 | +Table: Customer |
| 5 | +
|
| 6 | ++-------------+---------+ |
| 7 | +| Column Name | Type | |
| 8 | ++-------------+---------+ |
| 9 | +| id | int | |
| 10 | +| name | varchar | |
| 11 | +| referee_id | int | |
| 12 | ++-------------+---------+ |
| 13 | +In SQL, id is the primary key column for this table. |
| 14 | +Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them. |
| 15 | +
|
| 16 | +
|
| 17 | +Find the names of the customer that are not referred by the customer with id = 2. |
| 18 | +
|
| 19 | +Return the result table in any order. |
| 20 | +
|
| 21 | +The result format is in the following example. |
| 22 | +
|
| 23 | +
|
| 24 | +
|
| 25 | +Example 1: |
| 26 | +
|
| 27 | +Input: |
| 28 | +Customer table: |
| 29 | ++----+------+------------+ |
| 30 | +| id | name | referee_id | |
| 31 | ++----+------+------------+ |
| 32 | +| 1 | Will | null | |
| 33 | +| 2 | Jane | null | |
| 34 | +| 3 | Alex | 2 | |
| 35 | +| 4 | Bill | null | |
| 36 | +| 5 | Zack | 1 | |
| 37 | +| 6 | Mark | 2 | |
| 38 | ++----+------+------------+ |
| 39 | +Output: |
| 40 | ++------+ |
| 41 | +| name | |
| 42 | ++------+ |
| 43 | +| Will | |
| 44 | +| Jane | |
| 45 | +| Bill | |
| 46 | +| Zack | |
| 47 | ++------+ |
| 48 | +*/ |
| 49 | + |
| 50 | +------------------------------------------------------------------------------- |
| 51 | + |
| 52 | +-- Solution: |
| 53 | + |
1 | 54 | SELECT name FROM Customer
|
2 | 55 | WHERE (referee_id IS NULL) OR (NOT referee_id = 2)
|
You can’t perform that action at this time.
0 commit comments