Skip to content

feat: update solutions to lc problems #1796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions solution/0100-0199/0184.Department Highest Salary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,42 +80,29 @@ Department 表:

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

**方法一:等值连接 + 子查询**

我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。

**方法二:等值连接 + 窗口函数**

我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用窗口函数 `rank()`,它可以为每个部门的每个员工分配一个排名,然后我们可以选择排名为 $1$ 的行即可。

<!-- tabs:start -->

### **SQL**

```sql
SELECT
Department.NAME AS Department,
Employee.NAME AS Employee,
Salary
FROM
Employee,
Department
WHERE
Employee.DepartmentId = Department.Id
AND (Employee.DepartmentId, Salary) IN (
SELECT DepartmentId, max(Salary)
FROM Employee
GROUP BY DepartmentId
);
```

```sql
# Write your MySQL query statement below
SELECT
d.NAME AS Department,
e1.NAME AS Employee,
e1.salary AS Salary
SELECT d.name AS department, e.name AS employee, salary
FROM
Employee AS e1
JOIN Department AS d ON e1.departmentId = d.id
Employee AS e
JOIN Department AS d ON e.departmentId = d.id
WHERE
e1.salary = (
SELECT
MAX(Salary)
FROM Employee AS e2
WHERE e2.departmentId = d.id
(d.id, salary) IN (
SELECT departmentId, max(salary)
FROM Employee
GROUP BY 1
);
```

Expand All @@ -124,17 +111,19 @@ WHERE
WITH
T AS (
SELECT
*,
d.name AS department,
e.name AS employee,
salary,
rank() OVER (
PARTITION BY departmentId
PARTITION BY d.name
ORDER BY salary DESC
) AS rk
FROM Employee
FROM
Employee AS e
JOIN Department AS d ON e.departmentId = d.id
)
SELECT d.name AS Department, t.name AS Employee, salary AS Salary
FROM
T AS t
JOIN Department AS d ON t.departmentId = d.id
SELECT department, employee, salary
FROM T
WHERE rk = 1;
```

Expand Down
59 changes: 24 additions & 35 deletions solution/0100-0199/0184.Department Highest Salary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,29 @@ Department table:

## Solutions

**Solution 1: Equi-Join + Subquery**

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.

**Solution 2: Equi-Join + Window Function**

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.

<!-- tabs:start -->

### **SQL**

```sql
SELECT
Department.NAME AS Department,
Employee.NAME AS Employee,
Salary
FROM
Employee,
Department
WHERE
Employee.DepartmentId = Department.Id
AND (Employee.DepartmentId, Salary) IN (
SELECT DepartmentId, max(Salary)
FROM Employee
GROUP BY DepartmentId
);
```

```sql
# Write your MySQL query statement below
SELECT
d.NAME AS Department,
e1.NAME AS Employee,
e1.salary AS Salary
SELECT d.name AS department, e.name AS employee, salary
FROM
Employee AS e1
JOIN Department AS d ON e1.departmentId = d.id
Employee AS e
JOIN Department AS d ON e.departmentId = d.id
WHERE
e1.salary = (
SELECT
MAX(Salary)
FROM Employee AS e2
WHERE e2.departmentId = d.id
(d.id, salary) IN (
SELECT departmentId, max(salary)
FROM Employee
GROUP BY 1
);
```

Expand All @@ -122,17 +109,19 @@ WHERE
WITH
T AS (
SELECT
*,
d.name AS department,
e.name AS employee,
salary,
rank() OVER (
PARTITION BY departmentId
PARTITION BY d.name
ORDER BY salary DESC
) AS rk
FROM Employee
FROM
Employee AS e
JOIN Department AS d ON e.departmentId = d.id
)
SELECT d.name AS Department, t.name AS Employee, salary AS Salary
FROM
T AS t
JOIN Department AS d ON t.departmentId = d.id
SELECT department, employee, salary
FROM T
WHERE rk = 1;
```

Expand Down
23 changes: 9 additions & 14 deletions solution/0100-0199/0184.Department Highest Salary/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
*,
rank() OVER (
PARTITION BY departmentId
ORDER BY salary DESC
) AS rk
FROM Employee
)
SELECT d.name AS Department, t.name AS Employee, salary AS Salary
SELECT d.name AS department, e.name AS employee, salary
FROM
T AS t
JOIN Department AS d ON t.departmentId = d.id
WHERE rk = 1;
Employee AS e
JOIN Department AS d ON e.departmentId = d.id
WHERE
(d.id, salary) IN (
SELECT departmentId, max(salary)
FROM Employee
GROUP BY 1
);
3 changes: 1 addition & 2 deletions solution/1000-1099/1077.Project Employees III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ Employee 表:
WITH
T AS (
SELECT
project_id,
employee_id,
*,
rank() OVER (
PARTITION BY project_id
ORDER BY experience_years DESC
Expand Down
7 changes: 5 additions & 2 deletions solution/1000-1099/1077.Project Employees III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Employee table:

## Solutions

**Solution 1: Inner Join + Window Function**

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.

<!-- tabs:start -->

### **SQL**
Expand All @@ -88,8 +92,7 @@ Employee table:
WITH
T AS (
SELECT
project_id,
employee_id,
*,
rank() OVER (
PARTITION BY project_id
ORDER BY experience_years DESC
Expand Down
3 changes: 1 addition & 2 deletions solution/1000-1099/1077.Project Employees III/Solution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
WITH
T AS (
SELECT
project_id,
employee_id,
*,
rank() OVER (
PARTITION BY project_id
ORDER BY experience_years DESC
Expand Down
Loading