Skip to content

Commit dc32115

Browse files
committed
Add solution 181 [sql]
inner join
1 parent 84a1f14 commit dc32115

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
3636
| 155 | [Min Stack](https://github.com/doocs/leetcode/tree/master/solution/155.Min%20Stack) | `Stack`, `Design` |
3737
| 175 | [Combine Two Tables](https://github.com/doocs/leetcode/tree/master/solution/175.Combine%20Two%20Tables) | `SQL` |
3838
| 176 | [Second Highest Salary](https://github.com/doocs/leetcode/tree/master/solution/176.Second%20Highest%20Salary) | `SQL` |
39+
| 181 | [Employees Earning More Than Their Managers](https://github.com/doocs/leetcode/tree/master/solution/181.Employees%20Earning%20More%20Than%20Their%20Managers) | `SQL` |
3940
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
4041
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
4142
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## 连续出现的数字
2+
### 题目描述
3+
4+
`Employee` 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。
5+
```
6+
+----+-------+--------+-----------+
7+
| Id | Name | Salary | ManagerId |
8+
+----+-------+--------+-----------+
9+
| 1 | Joe | 70000 | 3 |
10+
| 2 | Henry | 80000 | 4 |
11+
| 3 | Sam | 60000 | NULL |
12+
| 4 | Max | 90000 | NULL |
13+
+----+-------+--------+-----------+
14+
```
15+
16+
给定 `Employee` 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
17+
```
18+
+----------+
19+
| Employee |
20+
+----------+
21+
| Joe |
22+
+----------+
23+
```
24+
25+
### 解法
26+
联表查询。
27+
28+
```sql
29+
# Write your MySQL query statement below
30+
31+
# select s.Name as Employee
32+
# from Employee s
33+
# where s.ManagerId is not null and s.Salary > (select Salary from Employee where Id = s.ManagerId);
34+
35+
36+
select s1.Name as Employee
37+
from Employee s1
38+
inner join Employee s2
39+
on s1.ManagerId = s2.Id
40+
where s1.Salary > s2.Salary;
41+
42+
```
43+
44+
#### Input
45+
```json
46+
{"headers": {"Employee": ["Id", "Name", "Salary", "ManagerId"]}, "rows": {"Employee": [[1, "Joe", 70000, 3], [2, "Henry", 80000, 4], [3, "Sam", 60000, null], [4, "Max", 90000, null]]}}
47+
```
48+
49+
#### Output
50+
```json
51+
{"headers":["Employee"],"values":[["Joe"]]}
52+
```

0 commit comments

Comments
 (0)