linye's Blog

全端工程師心得分享

0%

[LeetCode]584. Find Customer Referee

SQL I 筆記撰寫計畫

敘述

這是 SQL I 的第一天第三個題目,總共有四題。

  • 難度: Easy
  • 花費時間: 1min
  • 題目

給你一張表 Customer ,找出裡面 referee_id 不是 2 的。

Table: Customer

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| referee_id | int |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.

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 |
+------+

筆記

簡單的 select function 如下:

1
2
3
select name from Customer
where referee_id <> 2 # 大於小於 2
or referee_id is null # null 並不大於小於 2 因此單獨處理

成績