Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c5bd26b

Browse files
feat: add solutions to lc problem: No.3338 (doocs#3684)
No.3338.Second Highest Salary II
1 parent 529d315 commit c5bd26b

File tree

8 files changed

+355
-0
lines changed

8 files changed

+355
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md
5+
tags:
6+
- 数据库
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3338. Second Highest Salary II 🔒](https://leetcode.cn/problems/second-highest-salary-ii)
12+
13+
[English Version](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md)
14+
15+
## 题目描述
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>employees</code></p>
20+
21+
<pre>
22+
+------------------+---------+
23+
| Column Name | Type |
24+
+------------------+---------+
25+
| emp_id | int |
26+
| salary | int |
27+
| dept | varchar |
28+
+------------------+---------+
29+
emp_id is the unique key for this table.
30+
Each row of this table contains information about an employee including their ID, name, manager, salary, department, start date, and building assignment.
31+
</pre>
32+
33+
<p>Write a solution to find the employees who earn the <strong>second-highest salary</strong> in each department. If <strong>multiple employees have the second-highest salary</strong>, <strong>include</strong> <strong>all employees</strong> with <strong>that salary</strong>.</p>
34+
35+
<p>Return <em>the result table</em> <em>ordered by</em> <code>emp_id</code> <em>in</em> <em><strong>ascending</strong></em> <em>order</em>.</p>
36+
37+
<p>The result format is in the following example.</p>
38+
39+
<p>&nbsp;</p>
40+
<p><strong class="example">Example:</strong></p>
41+
42+
<div class="example-block">
43+
<p><strong>Input:</strong></p>
44+
45+
<p>employees table:</p>
46+
47+
<pre class="example-io">
48+
+--------+--------+-----------+
49+
| emp_id | salary | dept |
50+
+--------+--------+-----------+
51+
| 1 | 70000 | Sales |
52+
| 2 | 80000 | Sales |
53+
| 3 | 80000 | Sales |
54+
| 4 | 90000 | Sales |
55+
| 5 | 55000 | IT |
56+
| 6 | 65000 | IT |
57+
| 7 | 65000 | IT |
58+
| 8 | 50000 | Marketing |
59+
| 9 | 55000 | Marketing |
60+
| 10 | 55000 | HR |
61+
+--------+--------+-----------+
62+
</pre>
63+
64+
<p><strong>Output:</strong></p>
65+
66+
<pre class="example-io">
67+
+--------+-----------+
68+
| emp_id | dept |
69+
+--------+-----------+
70+
| 2 | Sales |
71+
| 3 | Sales |
72+
| 5 | IT |
73+
| 8 | Marketing |
74+
+--------+-----------+
75+
</pre>
76+
77+
<p><strong>Explanation:</strong></p>
78+
79+
<ul>
80+
<li><strong>Sales Department</strong>:
81+
82+
<ul>
83+
<li>Highest salary is 90000 (emp_id: 4)</li>
84+
<li>Second-highest salary is 80000 (emp_id: 2, 3)</li>
85+
<li>Both employees with salary 80000 are included</li>
86+
</ul>
87+
</li>
88+
<li><strong>IT Department</strong>:
89+
<ul>
90+
<li>Highest salary is 65000 (emp_id: 6, 7)</li>
91+
<li>Second-highest salary is 55000 (emp_id: 5)</li>
92+
<li>Only emp_id 5 is included as they have the second-highest salary</li>
93+
</ul>
94+
</li>
95+
<li><strong>Marketing Department</strong>:
96+
<ul>
97+
<li>Highest salary is 55000 (emp_id: 9)</li>
98+
<li>Second-highest salary is 50000 (emp_id: 8)</li>
99+
<li>Employee 8&nbsp;is included</li>
100+
</ul>
101+
</li>
102+
<li><strong>HR Department</strong>:
103+
<ul>
104+
<li>Only has one employee</li>
105+
<li>Not included in the result as it has fewer than 2 employees</li>
106+
</ul>
107+
</li>
108+
109+
</ul>
110+
</div>
111+
112+
<!-- description:end -->
113+
114+
## 解法
115+
116+
<!-- solution:start -->
117+
118+
### 方法一:窗口函数
119+
120+
我们可以使用 `DENSE_RANK()` 窗口函数来为每个部门的员工按照工资降序排名,然后筛选出排名为 2ドル$ 的员工即可。
121+
122+
<!-- tabs:start -->
123+
124+
#### MySQL
125+
126+
```sql
127+
# Write your MySQL query statement below
128+
WITH
129+
T AS (
130+
SELECT
131+
emp_id,
132+
dept,
133+
DENSE_RANK() OVER (
134+
PARTITION BY dept
135+
ORDER BY salary DESC
136+
) rk
137+
FROM Employees
138+
)
139+
SELECT emp_id, dept
140+
FROM T
141+
WHERE rk = 2
142+
ORDER BY 1;
143+
```
144+
145+
#### Pandas
146+
147+
```python
148+
import pandas as pd
149+
150+
151+
def find_second_highest_salary(employees: pd.DataFrame) -> pd.DataFrame:
152+
employees["rk"] = employees.groupby("dept")["salary"].rank(
153+
method="dense", ascending=False
154+
)
155+
second_highest = employees[employees["rk"] == 2][["emp_id", "dept"]]
156+
return second_highest.sort_values(by="emp_id")
157+
```
158+
159+
<!-- tabs:end -->
160+
161+
<!-- solution:end -->
162+
163+
<!-- problem:end -->
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md
5+
tags:
6+
- Database
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3338. Second Highest Salary II 🔒](https://leetcode.com/problems/second-highest-salary-ii)
12+
13+
[中文文档](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md)
14+
15+
## Description
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>employees</code></p>
20+
21+
<pre>
22+
+------------------+---------+
23+
| Column Name | Type |
24+
+------------------+---------+
25+
| emp_id | int |
26+
| salary | int |
27+
| dept | varchar |
28+
+------------------+---------+
29+
emp_id is the unique key for this table.
30+
Each row of this table contains information about an employee including their ID, name, manager, salary, department, start date, and building assignment.
31+
</pre>
32+
33+
<p>Write a solution to find the employees who earn the <strong>second-highest salary</strong> in each department. If <strong>multiple employees have the second-highest salary</strong>, <strong>include</strong> <strong>all employees</strong> with <strong>that salary</strong>.</p>
34+
35+
<p>Return <em>the result table</em> <em>ordered by</em> <code>emp_id</code> <em>in</em> <em><strong>ascending</strong></em> <em>order</em>.</p>
36+
37+
<p>The result format is in the following example.</p>
38+
39+
<p>&nbsp;</p>
40+
<p><strong class="example">Example:</strong></p>
41+
42+
<div class="example-block">
43+
<p><strong>Input:</strong></p>
44+
45+
<p>employees table:</p>
46+
47+
<pre class="example-io">
48+
+--------+--------+-----------+
49+
| emp_id | salary | dept |
50+
+--------+--------+-----------+
51+
| 1 | 70000 | Sales |
52+
| 2 | 80000 | Sales |
53+
| 3 | 80000 | Sales |
54+
| 4 | 90000 | Sales |
55+
| 5 | 55000 | IT |
56+
| 6 | 65000 | IT |
57+
| 7 | 65000 | IT |
58+
| 8 | 50000 | Marketing |
59+
| 9 | 55000 | Marketing |
60+
| 10 | 55000 | HR |
61+
+--------+--------+-----------+
62+
</pre>
63+
64+
<p><strong>Output:</strong></p>
65+
66+
<pre class="example-io">
67+
+--------+-----------+
68+
| emp_id | dept |
69+
+--------+-----------+
70+
| 2 | Sales |
71+
| 3 | Sales |
72+
| 5 | IT |
73+
| 8 | Marketing |
74+
+--------+-----------+
75+
</pre>
76+
77+
<p><strong>Explanation:</strong></p>
78+
79+
<ul>
80+
<li><strong>Sales Department</strong>:
81+
82+
<ul>
83+
<li>Highest salary is 90000 (emp_id: 4)</li>
84+
<li>Second-highest salary is 80000 (emp_id: 2, 3)</li>
85+
<li>Both employees with salary 80000 are included</li>
86+
</ul>
87+
</li>
88+
<li><strong>IT Department</strong>:
89+
<ul>
90+
<li>Highest salary is 65000 (emp_id: 6, 7)</li>
91+
<li>Second-highest salary is 55000 (emp_id: 5)</li>
92+
<li>Only emp_id 5 is included as they have the second-highest salary</li>
93+
</ul>
94+
</li>
95+
<li><strong>Marketing Department</strong>:
96+
<ul>
97+
<li>Highest salary is 55000 (emp_id: 9)</li>
98+
<li>Second-highest salary is 50000 (emp_id: 8)</li>
99+
<li>Employee 8&nbsp;is included</li>
100+
</ul>
101+
</li>
102+
<li><strong>HR Department</strong>:
103+
<ul>
104+
<li>Only has one employee</li>
105+
<li>Not included in the result as it has fewer than 2 employees</li>
106+
</ul>
107+
</li>
108+
109+
</ul>
110+
</div>
111+
112+
<!-- description:end -->
113+
114+
## Solutions
115+
116+
<!-- solution:start -->
117+
118+
### Solution 1: Window Function
119+
120+
We can use the `DENSE_RANK()` window function to rank employees in each department by salary in descending order, and then filter out the employees with a rank of 2ドル$.
121+
122+
<!-- tabs:start -->
123+
124+
#### MySQL
125+
126+
```sql
127+
# Write your MySQL query statement below
128+
WITH
129+
T AS (
130+
SELECT
131+
emp_id,
132+
dept,
133+
DENSE_RANK() OVER (
134+
PARTITION BY dept
135+
ORDER BY salary DESC
136+
) rk
137+
FROM Employees
138+
)
139+
SELECT emp_id, dept
140+
FROM T
141+
WHERE rk = 2
142+
ORDER BY 1;
143+
```
144+
145+
#### Pandas
146+
147+
```python
148+
import pandas as pd
149+
150+
151+
def find_second_highest_salary(employees: pd.DataFrame) -> pd.DataFrame:
152+
employees["rk"] = employees.groupby("dept")["salary"].rank(
153+
method="dense", ascending=False
154+
)
155+
second_highest = employees[employees["rk"] == 2][["emp_id", "dept"]]
156+
return second_highest.sort_values(by="emp_id")
157+
```
158+
159+
<!-- tabs:end -->
160+
161+
<!-- solution:end -->
162+
163+
<!-- problem:end -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pandas as pd
2+
3+
4+
def find_second_highest_salary(employees: pd.DataFrame) -> pd.DataFrame:
5+
employees["rk"] = employees.groupby("dept")["salary"].rank(
6+
method="dense", ascending=False
7+
)
8+
second_highest = employees[employees["rk"] == 2][["emp_id", "dept"]]
9+
return second_highest.sort_values(by="emp_id")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
emp_id,
6+
dept,
7+
DENSE_RANK() OVER (
8+
PARTITION BY dept
9+
ORDER BY salary DESC
10+
) rk
11+
FROM Employees
12+
)
13+
SELECT emp_id, dept
14+
FROM T
15+
WHERE rk = 2
16+
ORDER BY 1;

‎solution/DATABASE_README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
| 3308 | [寻找表现最佳的司机](/solution/3300-3399/3308.Find%20Top%20Performing%20Driver/README.md) | `数据库` | 中等 | 🔒 |
300300
| 3322 | [英超积分榜排名 III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README.md) | `数据库` | 中等 | 🔒 |
301301
| 3328 | [查找每个州的城市 II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | `数据库` | 中等 | 🔒 |
302+
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |
302303

303304
## 版权
304305

‎solution/DATABASE_README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
297297
| 3308 | [Find Top Performing Driver](/solution/3300-3399/3308.Find%20Top%20Performing%20Driver/README_EN.md) | `Database` | Medium | 🔒 |
298298
| 3322 | [Premier League Table Ranking III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README_EN.md) | `Database` | Medium | 🔒 |
299299
| 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md) | `Database` | Medium | 🔒 |
300+
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |
300301

301302
## Copyright
302303

‎solution/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,7 @@
33483348
| 3335 | [字符串转换后的长度 I](/solution/3300-3399/3335.Total%20Characters%20in%20String%20After%20Transformations%20I/README.md) | | 中等 | 第 421 场周赛 |
33493349
| 3336 | [最大公约数相等的子序列数量](/solution/3300-3399/3336.Find%20the%20Number%20of%20Subsequences%20With%20Equal%20GCD/README.md) | | 困难 | 第 421 场周赛 |
33503350
| 3337 | [字符串转换后的长度 II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README.md) | | 困难 | 第 421 场周赛 |
3351+
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |
33513352

33523353
## 版权
33533354

‎solution/README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,6 +3346,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
33463346
| 3335 | [Total Characters in String After Transformations I](/solution/3300-3399/3335.Total%20Characters%20in%20String%20After%20Transformations%20I/README_EN.md) | | Medium | Weekly Contest 421 |
33473347
| 3336 | [Find the Number of Subsequences With Equal GCD](/solution/3300-3399/3336.Find%20the%20Number%20of%20Subsequences%20With%20Equal%20GCD/README_EN.md) | | Hard | Weekly Contest 421 |
33483348
| 3337 | [Total Characters in String After Transformations II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README_EN.md) | | Hard | Weekly Contest 421 |
3349+
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |
33493350

33503351
## Copyright
33513352

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /