Skip to content

Commit e0b9202

Browse files
committed
Add solution 176 [sql]
1 parent 8520ae2 commit e0b9202

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
3535
| 141 | [Linked List Cycle](https://github.com/doocs/leetcode/tree/master/solution/141.Linked%20List%20Cycle) | `Linked List`, `Two Pointers` |
3636
| 155 | [Min Stack](https://github.com/doocs/leetcode/tree/master/solution/155.Min%20Stack) | `Stack`, `Design` |
3737
| 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` |
3839
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
3940
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
4041
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
```

0 commit comments

Comments
 (0)