Skip to content

Commit 82eb65c

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 67db8e4 commit 82eb65c

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@
329329

330330
[183. Customers Who Never Order](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/183.%20Customers%20Who%20Never%20Order.md)
331331

332+
[184. Department Highest Salary](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/184.%20Department%20Highest%20Salary.md)
333+
334+
[185. Department Top Three Salaries](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/185.%20Department%20Top%20Three%20Salaries.md)
335+
332336
## Algorithm(4th_Edition)
333337
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
334338
All java realization codes are placed in different packages.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## 184. Department Highest Salary
2+
3+
### Question
4+
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
5+
6+
```
7+
+----+-------+--------+--------------+
8+
| Id | Name | Salary | DepartmentId |
9+
+----+-------+--------+--------------+
10+
| 1 | Joe | 70000 | 1 |
11+
| 2 | Henry | 80000 | 2 |
12+
| 3 | Sam | 60000 | 2 |
13+
| 4 | Max | 90000 | 1 |
14+
+----+-------+--------+--------------+
15+
```
16+
17+
The Department table holds all departments of the company.
18+
```
19+
+----+----------+
20+
| Id | Name |
21+
+----+----------+
22+
| 1 | IT |
23+
| 2 | Sales |
24+
+----+----------+
25+
```
26+
Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.
27+
```
28+
+------------+----------+--------+
29+
| Department | Employee | Salary |
30+
+------------+----------+--------+
31+
| IT | Max | 90000 |
32+
| Sales | Henry | 80000 |
33+
+------------+----------+--------+
34+
```
35+
36+
### 二刷
37+
1. 使用join将两张表连接起来。使用聚合函数筛选出最大值,最后通过in来选取。
38+
* 思考:从哪张表连接向哪张表?我们要选出部门中的最大工资,所以使用部门最为左表。
39+
```MYSQL
40+
# Write your MySQL query statement below
41+
SELECT d.Name AS Department,
42+
e.Name as Employee,
43+
e.Salary as Salary
44+
FROM Department d #通过外连接将两张表合并
45+
LEFT JOIN Employee e
46+
ON d.Id = e.DepartmentId
47+
WHERE (d.Id, e.Salary) IN ( #通过IN判断出应该显示哪些字段
48+
SELECT e.DepartmentId, max(e.Salary) from Employee e GROUP BY e.DepartmentId #使用聚合函数筛选出最大值
49+
);
50+
```
51+
52+
### 引用
53+
1.[leetcode-184-Department Highest Salary 优化记录](https://www.cnblogs.com/zhangyunhao/p/4896055.html)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 185. Department Top Three Salaries
2+
3+
### Question
4+
The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.
5+
```
6+
+----+-------+--------+--------------+
7+
| Id | Name | Salary | DepartmentId |
8+
+----+-------+--------+--------------+
9+
| 1 | Joe | 70000 | 1 |
10+
| 2 | Henry | 80000 | 2 |
11+
| 3 | Sam | 60000 | 2 |
12+
| 4 | Max | 90000 | 1 |
13+
| 5 | Janet | 69000 | 1 |
14+
| 6 | Randy | 85000 | 1 |
15+
+----+-------+--------+--------------+
16+
```
17+
The Department table holds all departments of the company.
18+
```
19+
+----+----------+
20+
| Id | Name |
21+
+----+----------+
22+
| 1 | IT |
23+
| 2 | Sales |
24+
+----+----------+
25+
```
26+
Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.
27+
```
28+
+------------+----------+--------+
29+
| Department | Employee | Salary |
30+
+------------+----------+--------+
31+
| IT | Max | 90000 |
32+
| IT | Randy | 85000 |
33+
| IT | Joe | 70000 |
34+
| Sales | Henry | 80000 |
35+
| Sales | Sam | 60000 |
36+
+------------+----------+--------+
37+
```
38+
39+
### 二刷
40+
1. 这道题和上一题比起来,需要选出当前部门中前几高的工资,所以我们使用了count函数,来选出当前部门中,大于该员工工资个数不大于3的所有员工。
41+
```MYSQL
42+
# Write your MySQL query statement below
43+
SELECT d.Name as Department,
44+
e.Name as Employee,
45+
e.Salary as Salary
46+
FROM Department d, Employee e
47+
WHERE(
48+
SELECT COUNT(distinct Salary)
49+
FROM Employee
50+
WHERE Salary > e.Salary AND DepartmentId = d.Id
51+
) < 3 AND e.DepartmentId = d.Id
52+
ORDER BY d.Id, e.Salary desc;
53+
```
54+
55+
### 引用
56+
1.[LeetCode] Department Top Three Salaries 系里前三高薪水](https://www.cnblogs.com/grandyang/p/5367670.html)

0 commit comments

Comments
 (0)