Skip to content

Commit 313b529

Browse files
author
SeanForFun
committed
[Function add]
1.Add leetcode solutions.
1 parent 3af2b3b commit 313b529

File tree

3 files changed

+40
-55
lines changed

3 files changed

+40
-55
lines changed

README.md

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

324324
[180. Consecutive Numbers](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/180.%20Consecutive%20Numbers.md)
325325

326+
[181. Employees Earning More Than Their Managers](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/181.%20EmployeesVEarning%20More%20Than%20Their%20Managers.md)
327+
328+
[182. Duplicate Emails](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/182.%20Duplicate%20Emails.md)
329+
326330
## Algorithm(4th_Edition)
327331
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
328332
All java realization codes are placed in different packages.

leetcode/181. Employees Earning More Than Their Managers.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
### Question
44
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
55

6-
+----+-------+--------+-----------+
6+
```
7+
----+-------+--------+-----------+
78
| Id | Name | Salary | ManagerId |
89
+----+-------+--------+-----------+
910
| 1 | Joe | 70000 | 3 |
1011
| 2 | Henry | 80000 | 4 |
1112
| 3 | Sam | 60000 | NULL |
1213
| 4 | Max | 90000 | NULL |
1314
+----+-------+--------+-----------+
15+
```
1416

1517
Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
1618

19+
```
1720
+----------+
1821
| Employee |
1922
+----------+
2023
| Joe |
2124
+----------+
25+
```
2226

2327
### Thinking:
2428
* Method: 左外连接,速度比where要快。
@@ -53,4 +57,24 @@ where
5357
where
5458
a.ManagerId = b.Id
5559
);
60+
```
61+
62+
# 二刷
63+
1. 使用左外连接,这样我们可以合并两张表的内容。
64+
2. 使用where做出匹配。
65+
```Sql
66+
# Write your MySQL query statement below
67+
SELECT a.Name as Employee
68+
FROM Employee a
69+
LEFT JOIN Employee b
70+
ON a.ManagerId = b.Id
71+
WHERE a.Salary > b.Salary;
72+
```
73+
74+
3. 将同一张表使用两次做出匹配。
75+
```SQL
76+
# Write your MySQL query statement below
77+
SELECT a.Name as Employee
78+
FROM Employee a, Employee b
79+
where a.ManagerId = b.Id and a.Salary > b.Salary;
5680
```

leetcode/182. Duplicate Emails.md

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
## 181. Employees Earning More Than Their Managers
22

3-
### Question
4-
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager 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-
Given the Employee table, a## 182. Duplicate Emails
16-
173
### Question
184
Write a SQL query to find all duplicate emails in a table named Person.
19-
5+
```
206
+----+---------+
217
| Id | Email |
228
+----+---------+
239
| 1 | a@b.com |
2410
| 2 | c@d.com |
2511
| 3 | a@b.com |
2612
+----+---------+
13+
```
2714

2815
For example, your query should return the following for the above table:
29-
16+
```
3017
+---------+
3118
| Email |
3219
+---------+
3320
| a@b.com |
3421
+---------+
22+
```
3523

3624
Note: All emails are in lowercase.
3725

@@ -48,45 +36,14 @@ group by
4836
Email
4937
having
5038
count(*) > 1
51-
```write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
52-
53-
+----------+
54-
| Employee |
55-
+----------+
56-
| Joe |
57-
+----------+
58-
59-
### Thinking:
60-
* Method: 左外连接,速度比where要快。
61-
62-
```SQL
63-
# Write your MySQL query statement below
64-
select
65-
a.Name as Employee
66-
from Employee a
67-
left join
68-
Employee b
69-
on
70-
a.ManagerID = b.Id
71-
where
72-
a.Salary > b.Salary
7339
```
7440

75-
* Method 2:通过查询确定判断条件,这种方法比较慢,经过两次查询,使用了两个session。
76-
77-
```Java
41+
### 二刷
42+
1. 使用Email字段作为分组的类别,将count()作为聚合函数。
43+
```SQL
7844
# Write your MySQL query statement below
79-
select
80-
name as Employee
81-
from
82-
Employee a
83-
where
84-
a.Salary > (
85-
select
86-
b.Salary
87-
from
88-
Employee b
89-
where
90-
a.ManagerId = b.Id
91-
);
45+
SELECT Email
46+
FROM Person
47+
GROUP BY Email
48+
HAVING count(*) > 1;
9249
```

0 commit comments

Comments
 (0)