File tree Expand file tree Collapse file tree 2 files changed +74
-0
lines changed
solution/176.Second Highest Salary Expand file tree Collapse file tree 2 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
35
35
| 141 | [ Linked List Cycle] ( https://github.com/doocs/leetcode/tree/master/solution/141.Linked%20List%20Cycle ) | ` Linked List ` , ` Two Pointers ` |
36
36
| 155 | [ Min Stack] ( https://github.com/doocs/leetcode/tree/master/solution/155.Min%20Stack ) | ` Stack ` , ` Design ` |
37
37
| 175 | [ Combine Two Tables] ( https://github.com/doocs/leetcode/tree/master/solution/175.Combine%20Two%20Tables ) | ` SQL ` |
38
+ | 176 | [ Second Highest Salary] ( https://github.com/doocs/leetcode/tree/master/solution/176.Second%20Highest%20Salary ) | ` SQL ` |
38
39
| 189 | [ Rotate Array] ( https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array ) | ` Array ` |
39
40
| 198 | [ House Robber] ( https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber ) | ` Dynamic Programming ` |
40
41
| 203 | [ Remove Linked List Elements] ( https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements ) | ` Linked List ` |
Original file line number Diff line number Diff line change
1
+ ## 第二高的薪水
2
+ ### 题目描述
3
+
4
+ 编写一个 SQL 查询,获取 ` Employee ` 表中第二高的薪水(Salary) 。
5
+ ```
6
+ +----+--------+
7
+ | Id | Salary |
8
+ +----+--------+
9
+ | 1 | 100 |
10
+ | 2 | 200 |
11
+ | 3 | 300 |
12
+ +----+--------+
13
+ ```
14
+
15
+ 例如上述 ` Employee ` 表,SQL查询应该返回 ` 200 ` 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 ` null ` 。
16
+ ```
17
+ +---------------------+
18
+ | SecondHighestSalary |
19
+ +---------------------+
20
+ | 200 |
21
+ +---------------------+
22
+ ```
23
+
24
+ ### 解法
25
+ 从小于最高薪水的记录里面选最高即可。
26
+
27
+ ``` sql
28
+ # Write your MySQL query statement below
29
+ select max (Salary) SecondHighestSalary from Employee where Salary < (Select max (Salary) from Employee)
30
+
31
+ ```
32
+
33
+ #### Input
34
+ ``` json
35
+ {
36
+ "headers" : {
37
+ "Employee" : [
38
+ " Id" ,
39
+ " Salary"
40
+ ]
41
+ },
42
+ "rows" : {
43
+ "Employee" : [
44
+ [
45
+ 1 ,
46
+ 100
47
+ ],
48
+ [
49
+ 2 ,
50
+ 200
51
+ ],
52
+ [
53
+ 3 ,
54
+ 300
55
+ ]
56
+ ]
57
+ }
58
+ }
59
+ ```
60
+
61
+ #### Output
62
+ ``` json
63
+ {
64
+ "headers" : [
65
+ " SecondHighestSalary"
66
+ ],
67
+ "values" : [
68
+ [
69
+ 200
70
+ ]
71
+ ]
72
+ }
73
+ ```
You can’t perform that action at this time.
0 commit comments