Skip to content

Commit 8766a9a

Browse files
author
luzhipeng
committed
feat: 每日一题 2019-06-21
1 parent b49034e commit 8766a9a

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

Diff for: daily/2019-06-21.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## 每日一题 - Nth Highest Salary
2+
3+
### 信息卡片
4+
5+
- 时间:2019-06-21
6+
- 题目链接:https://leetcode.com/problems/nth-highest-salary/
7+
- tag:`sql`
8+
9+
### 题目描述
10+
11+
```
12+
Write a SQL query to get the nth highest salary from the Employee table.
13+
14+
+----+--------+
15+
| Id | Salary |
16+
+----+--------+
17+
| 1 | 100 |
18+
| 2 | 200 |
19+
| 3 | 300 |
20+
+----+--------+
21+
For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
22+
23+
+------------------------+
24+
| getNthHighestSalary(2) |
25+
+------------------------+
26+
| 200 |
27+
+------------------------+
28+
```
29+
30+
### 参考答案
31+
32+
这是一个简单的考察sql基本操作的题目,我们只要orderby desc一下然后limit选取指定项即可。
33+
34+
需要注意的是sql中好像没有N-1这样的操作,
35+
因此需要类似这样:
36+
37+
```
38+
DECLARE M INT;
39+
SET M=N-1;
40+
41+
```
42+
43+
44+
参考代码:
45+
46+
```sql
47+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
48+
BEGIN
49+
DECLARE M INT;
50+
SET M=N-1;
51+
RETURN (
52+
# Write your MySQL query statement below.
53+
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
54+
);
55+
END
56+
57+
```
58+
59+
### 其他优秀解答
60+
```
61+
暂无
62+
```

Diff for: daily/README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,26 @@ tag: `Tree`
4141

4242
时间: 2019-06-10
4343

44-
### [三门问题](./2019-06-13.md)
45-
46-
tag: `Probability Theory`
47-
48-
时间: 2019-06-13
49-
5044
### [重复数据排序优化](./2019-06-11.md)
5145

5246
tag: `Sort` `Quick Sort`
5347

5448
时间: 2019-06-11
5549

50+
### [三门问题](./2019-06-13.md)
51+
52+
tag: `Probability Theory`
53+
54+
时间: 2019-06-13
55+
5656
### [114.flatten-binary-tree-to-linked-list](./2019-06-14.md)
5757

5858
tag: `tree`
5959

6060
时间: 2019-06-14
61+
62+
### [114.flatten-binary-tree-to-linked-list](./2019-06-21.md)
63+
64+
tag: `sql`
65+
66+
时间: 2019-06-21

0 commit comments

Comments
 (0)