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 0e4c841

Browse files
feat: update solutions to lc problems (doocs#1796)
* No.0184.Department Highest Salary * No.1077.Project Employees III * No.1488.Avoid Flood in The City * No.1532.The Most Recent Three Orders * No.1549.The Most Recent Orders for Each Product * No.1831.Maximum Transaction Each Day
1 parent 8e6eeaf commit 0e4c841

File tree

19 files changed

+1789
-237
lines changed

19 files changed

+1789
-237
lines changed

‎solution/0100-0199/0184.Department Highest Salary/README.md‎

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,42 +80,29 @@ Department 表:
8080

8181
<!-- 这里可写通用的实现逻辑 -->
8282

83+
**方法一:等值连接 + 子查询**
84+
85+
我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。
86+
87+
**方法二:等值连接 + 窗口函数**
88+
89+
我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用窗口函数 `rank()`,它可以为每个部门的每个员工分配一个排名,然后我们可以选择排名为 1ドル$ 的行即可。
90+
8391
<!-- tabs:start -->
8492

8593
### **SQL**
8694

87-
```sql
88-
SELECT
89-
Department.NAME AS Department,
90-
Employee.NAME AS Employee,
91-
Salary
92-
FROM
93-
Employee,
94-
Department
95-
WHERE
96-
Employee.DepartmentId = Department.Id
97-
AND (Employee.DepartmentId, Salary) IN (
98-
SELECT DepartmentId, max(Salary)
99-
FROM Employee
100-
GROUP BY DepartmentId
101-
);
102-
```
103-
10495
```sql
10596
# Write your MySQL query statement below
106-
SELECT
107-
d.NAME AS Department,
108-
e1.NAME AS Employee,
109-
e1.salary AS Salary
97+
SELECT d.name AS department, e.name AS employee, salary
11098
FROM
111-
Employee AS e1
112-
JOIN Department AS d ON e1.departmentId = d.id
99+
Employee AS e
100+
JOIN Department AS d ON e.departmentId = d.id
113101
WHERE
114-
e1.salary = (
115-
SELECT
116-
MAX(Salary)
117-
FROM Employee AS e2
118-
WHERE e2.departmentId = d.id
102+
(d.id, salary) IN (
103+
SELECT departmentId, max(salary)
104+
FROM Employee
105+
GROUP BY 1
119106
);
120107
```
121108

@@ -124,17 +111,19 @@ WHERE
124111
WITH
125112
T AS (
126113
SELECT
127-
*,
114+
d.name AS department,
115+
e.name AS employee,
116+
salary,
128117
rank() OVER (
129-
PARTITION BY departmentId
118+
PARTITION BY d.name
130119
ORDER BY salary DESC
131120
) AS rk
132-
FROM Employee
121+
FROM
122+
Employee AS e
123+
JOIN Department AS d ON e.departmentId = d.id
133124
)
134-
SELECT d.name AS Department, t.name AS Employee, salary AS Salary
135-
FROM
136-
T AS t
137-
JOIN Department AS d ON t.departmentId = d.id
125+
SELECT department, employee, salary
126+
FROM T
138127
WHERE rk = 1;
139128
```
140129

‎solution/0100-0199/0184.Department Highest Salary/README_EN.md‎

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,29 @@ Department table:
7878

7979
## Solutions
8080

81+
**Solution 1: Equi-Join + Subquery**
82+
83+
We can use an equi-join to join the `Employee` table and the `Department` table based on `Employee.departmentId = Department.id`, and then use a subquery to find the highest salary for each department. Finally, we can use a `WHERE` clause to filter out the employees with the highest salary in each department.
84+
85+
**Solution 2: Equi-Join + Window Function**
86+
87+
We can use an equi-join to join the `Employee` table and the `Department` table based on `Employee.departmentId = Department.id`, and then use the window function `rank()`, which assigns a rank to each employee in each department based on their salary. Finally, we can select the rows with a rank of 1ドル$ for each department.
88+
8189
<!-- tabs:start -->
8290

8391
### **SQL**
8492

85-
```sql
86-
SELECT
87-
Department.NAME AS Department,
88-
Employee.NAME AS Employee,
89-
Salary
90-
FROM
91-
Employee,
92-
Department
93-
WHERE
94-
Employee.DepartmentId = Department.Id
95-
AND (Employee.DepartmentId, Salary) IN (
96-
SELECT DepartmentId, max(Salary)
97-
FROM Employee
98-
GROUP BY DepartmentId
99-
);
100-
```
101-
10293
```sql
10394
# Write your MySQL query statement below
104-
SELECT
105-
d.NAME AS Department,
106-
e1.NAME AS Employee,
107-
e1.salary AS Salary
95+
SELECT d.name AS department, e.name AS employee, salary
10896
FROM
109-
Employee AS e1
110-
JOIN Department AS d ON e1.departmentId = d.id
97+
Employee AS e
98+
JOIN Department AS d ON e.departmentId = d.id
11199
WHERE
112-
e1.salary = (
113-
SELECT
114-
MAX(Salary)
115-
FROM Employee AS e2
116-
WHERE e2.departmentId = d.id
100+
(d.id, salary) IN (
101+
SELECT departmentId, max(salary)
102+
FROM Employee
103+
GROUP BY 1
117104
);
118105
```
119106

@@ -122,17 +109,19 @@ WHERE
122109
WITH
123110
T AS (
124111
SELECT
125-
*,
112+
d.name AS department,
113+
e.name AS employee,
114+
salary,
126115
rank() OVER (
127-
PARTITION BY departmentId
116+
PARTITION BY d.name
128117
ORDER BY salary DESC
129118
) AS rk
130-
FROM Employee
119+
FROM
120+
Employee AS e
121+
JOIN Department AS d ON e.departmentId = d.id
131122
)
132-
SELECT d.name AS Department, t.name AS Employee, salary AS Salary
133-
FROM
134-
T AS t
135-
JOIN Department AS d ON t.departmentId = d.id
123+
SELECT department, employee, salary
124+
FROM T
136125
WHERE rk = 1;
137126
```
138127

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# Write your MySQL query statement below
2-
WITH
3-
T AS (
4-
SELECT
5-
*,
6-
rank() OVER (
7-
PARTITION BY departmentId
8-
ORDER BY salary DESC
9-
) AS rk
10-
FROM Employee
11-
)
12-
SELECT d.name AS Department, t.name AS Employee, salary AS Salary
2+
SELECT d.name AS department, e.name AS employee, salary
133
FROM
14-
T AS t
15-
JOIN Department AS d ON t.departmentId = d.id
16-
WHERE rk = 1;
4+
Employee AS e
5+
JOIN Department AS d ON e.departmentId = d.id
6+
WHERE
7+
(d.id, salary) IN (
8+
SELECT departmentId, max(salary)
9+
FROM Employee
10+
GROUP BY 1
11+
);

‎solution/1000-1099/1077.Project Employees III/README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ Employee 表:
9595
WITH
9696
T AS (
9797
SELECT
98-
project_id,
99-
employee_id,
98+
*,
10099
rank() OVER (
101100
PARTITION BY project_id
102101
ORDER BY experience_years DESC

‎solution/1000-1099/1077.Project Employees III/README_EN.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ Employee table:
7979

8080
## Solutions
8181

82+
**Solution 1: Inner Join + Window Function**
83+
84+
We can first perform an inner join between the `Project` table and the `Employee` table, and then use the window function `rank()` to group the `Project` table, sort it in descending order by `experience_years`, and finally select the most experienced employee for each project.
85+
8286
<!-- tabs:start -->
8387

8488
### **SQL**
@@ -88,8 +92,7 @@ Employee table:
8892
WITH
8993
T AS (
9094
SELECT
91-
project_id,
92-
employee_id,
95+
*,
9396
rank() OVER (
9497
PARTITION BY project_id
9598
ORDER BY experience_years DESC

‎solution/1000-1099/1077.Project Employees III/Solution.sql‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
WITH
33
T AS (
44
SELECT
5-
project_id,
6-
employee_id,
5+
*,
76
rank() OVER (
87
PARTITION BY project_id
98
ORDER BY experience_years DESC

0 commit comments

Comments
(0)

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