Skip to content

Commit bb4a5df

Browse files
committed
Add solution 184 [sql]
group by inner join
1 parent ecb45d1 commit bb4a5df

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
9494
| 177 | [Nth Highest Salary](https://github.com/doocs/leetcode/tree/master/solution/177.Nth%20Highest%20Salary) | `SQL` |
9595
| 178 | [Rank Scores](https://github.com/doocs/leetcode/tree/master/solution/178.Rank%20Scores) | `SQL` |
9696
| 180 | [Consecutive Numbers](https://github.com/doocs/leetcode/tree/master/solution/180.Consecutive%20Numbers) | `SQL` |
97+
| 184 | [Department Highest Salary](https://github.com/doocs/leetcode/tree/master/solution/184.Department%20Highest%20Salary) | `SQL` |
9798
| 328 | [Odd Even Linked List](https://github.com/doocs/leetcode/tree/master/solution/328.Odd%20Even%20Linked%20List) | `Linked List` |
9899

99100

@@ -125,6 +126,6 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
125126
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
126127
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31923541?v=4" width="80px;"/>](https://github.com/zouwx2cs) </center> |
127128
|---|---|---|---|---|---|---|---|
128-
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44309823?v=4" width="80px;"/>](https://github.com/KongJHong) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/18181519?v=4" width="80px;"/>](https://github.com/limbowandering) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37685012?v=4" width="80px;"/>](https://github.com/jxdeng3989) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44314231?v=4" width="80px;"/>](https://github.com/igayhub) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/30177307?v=4" width="80px;"/>](https://github.com/MCN1998) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/5793058?v=4" width="80px;"/>](https://github.com/Fairyhead) </center> |
129+
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44309823?v=4" width="80px;"/>](https://github.com/KongJHong) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/18181519?v=4" width="80px;"/>](https://github.com/limbowandering) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37685012?v=4" width="80px;"/>](https://github.com/jxdeng3989) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44314231?v=4" width="80px;"/>](https://github.com/igayhub) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/30177307?v=4" width="80px;"/>](https://github.com/MCN1998) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/5793058?v=4" width="80px;"/>](https://github.com/Fairyhead) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/24841082?v=4" width="80px;"/>](https://github.com/zhng1456) </center> |
129130

130131
<!-- ALL-CONTRIBUTORS-LIST:END -->

SOLUTION_TREE.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
│   │   ├── README.md
2222
│   │   ├── Solution.cpp
2323
│   │   ├── Solution.java
24+
│   │   ├── Solution.js
2425
│   │   ├── Solution.py
2526
│   │   └── Solution.rb
2627
│   ├── 003.Longest Substring Without Repeating Characters
@@ -77,6 +78,7 @@
7778
│   │   ├── README.md
7879
│   │   ├── Solution.cpp
7980
│   │   ├── Solution.java
81+
│   │   ├── Solution.js
8082
│   │   ├── Solution.py
8183
│   │   └── Solution.rb
8284
│   ├── 021.Merge Two Sorted Lists
@@ -92,7 +94,8 @@
9294
│   ├── 024.Swap Nodes in Pairs
9395
│   │   ├── README.md
9496
│   │   ├── Solution.java
95-
│   │   └── Solution.py
97+
│   │   ├── Solution.py
98+
│   │   └── Solution.rb
9699
│   ├── 025.Reverse Nodes in k-Group
97100
│   │   ├── README.md
98101
│   │   └── Solution.java
@@ -105,6 +108,9 @@
105108
│   ├── 028.Implement strStr()
106109
│   │   ├── README.md
107110
│   │   └── Solution.java
111+
│   ├── 029.Divide Two Integers
112+
│   │   ├── README.md
113+
│   │   └── Solution.java
108114
│   ├── 031.Next Permutation
109115
│   │   ├── README.md
110116
│   │   └── Solution.java
@@ -213,6 +219,9 @@
213219
│   ├── 145.Binary Tree Postorder Traversal
214220
│   │   ├── README.md
215221
│   │   └── Solution.java
222+
│   ├── 146.Lru Cache
223+
│   │   ├── README.md
224+
│   │   └── Solution.java
216225
│   ├── 150.Evaluate Reverse Polish Notation
217226
│   │   ├── README.md
218227
│   │   └── Solution.java
@@ -240,11 +249,18 @@
240249
│   │   └── README.md
241250
│   ├── 180.Consecutive Numbers
242251
│   │   └── README.md
252+
│   ├── 181.Employees Earning More Than Their Managers
253+
│   │   └── README.md
254+
│   ├── 182.Duplicate Emails
255+
│   │   └── README.md
256+
│   ├── 183.Customers Who Never Order
257+
│   │   └── README.md
243258
│   ├── 189.Rotate Array
244259
│   │   ├── README.md
245260
│   │   └── Solution.java
246261
│   ├── 190.Reverse Bits
247-
│   │   └── Solution.cpp
262+
│   │   ├── Solution.cpp
263+
│   │   └── Solution.js
248264
│   ├── 191.Number of 1 Bits
249265
│   │   └── Solution.js
250266
│   ├── 198.House Robber
@@ -292,6 +308,8 @@
292308
│   │   └── Solution.cpp
293309
│   ├── 443.String Compression
294310
│   │   └── Solution.cpp
311+
│   ├── 461.Hamming Distance
312+
│   │   └── Solution.js
295313
│   ├── 581.Shortest Unsorted Continuous Subarray
296314
│   │   ├── README.md
297315
│   │   ├── Solution.cpp
@@ -304,6 +322,8 @@
304322
│   │   ├── README.md
305323
│   │   ├── Solution.cpp
306324
│   │   └── Solution.java
325+
│   ├── 627.Swap Salary
326+
│   │   └── README.md
307327
│   ├── 695.Max Area of Island
308328
│   │   ├── README.md
309329
│   │   ├── Solution.cpp
@@ -325,6 +345,4 @@
325345
│   └── 926.Flip String to Monotone Increasing
326346
│   └── Solution.js
327347
└── SOLUTION_TREE.md
328-
329-
330348
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## 部门工资最高的员工
2+
### 题目描述
3+
4+
`Employee` 表包含所有员工信息,每个员工有其对应的 Id, salary 和 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+
+----+-------+--------+--------------+
14+
```
15+
16+
`Department` 表包含公司所有部门的信息。
17+
```
18+
+----+----------+
19+
| Id | Name |
20+
+----+----------+
21+
| 1 | IT |
22+
| 2 | Sales |
23+
+----+----------+
24+
```
25+
26+
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
27+
```
28+
+------------+----------+--------+
29+
| Department | Employee | Salary |
30+
+------------+----------+--------+
31+
| IT | Max | 90000 |
32+
| Sales | Henry | 80000 |
33+
+------------+----------+--------+
34+
```
35+
36+
### 解法
37+
先对 `Employee` 表根据 `DepartmentId` 进行分组,找出每组中工资最高的员工,再与 `Department` 表进行内连接,即可查出结果。
38+
39+
```sql
40+
# Write your MySQL query statement below
41+
42+
select d.Name as Department, c.Name as Employee, c.Salary
43+
from (select a.Name, a.DepartmentId, a.Salary from Employee a
44+
inner join (select DepartmentId, max(Salary) Salary from Employee group by DepartmentId) b
45+
on a.DepartMentId = b.DepartMentId and a.Salary = b.Salary) c
46+
inner join DepartMent d
47+
on d.Id = c.DepartmentId;
48+
```
49+
50+
#### Input
51+
```json
52+
{"headers": {"Employee": ["Id", "Name", "Salary", "DepartmentId"], "Department": ["Id", "Name"]}, "rows": {"Employee": [[1, "Joe", 70000, 1], [2, "Henry", 80000, 2], [3, "Sam", 60000, 2], [4, "Max", 90000, 1]], "Department": [[1, "IT"], [2, "Sales"]]}}
53+
```
54+
55+
#### Output
56+
```json
57+
{"headers":["Department","Employee","Salary"],"values":[["Sales","Henry",80000],["IT","Max",90000]]}
58+
```

0 commit comments

Comments
 (0)