Skip to content

Commit 78fc1fb

Browse files
authored
feat: update solutions to lc problems: No.1907,1978 (doocs#1878)
* No.1907.Count Salary Categories * No.1978.Employees Whose Manager Left the Company
1 parent 59b8e99 commit 78fc1fb

File tree

6 files changed

+85
-29
lines changed

6 files changed

+85
-29
lines changed

solution/1900-1999/1907.Count Salary Categories/README.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ Accounts 表:
6969

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

72+
**方法一:构建临时表 + 分组统计 + 左连接**
73+
74+
我们可以先构建一个临时表,包含所有工资类别,然后再统计每个工资类别的银行账户数量。最后我们使用左连接,将临时表和统计结果表连接起来,这样就可以保证结果表中包含所有工资类别。
75+
76+
**方法二:筛选 + 合并**
77+
78+
我们可以分别筛选出每个工资类别的银行账户数量,然后再将结果合并起来。这里我们使用 `UNION` 来合并结果。
79+
7280
<!-- tabs:start -->
7381

7482
### **SQL**
@@ -94,12 +102,24 @@ WITH
94102
END AS category,
95103
COUNT(1) AS accounts_count
96104
FROM Accounts
97-
GROUP BY category
105+
GROUP BY 1
98106
)
99-
SELECT s.category, IFNULL(accounts_count, 0) AS accounts_count
107+
SELECT category, IFNULL(accounts_count, 0) AS accounts_count
100108
FROM
101-
S AS s
102-
LEFT JOIN T AS t ON s.category = t.category;
109+
S
110+
LEFT JOIN T USING (category);
111+
```
112+
113+
```sql
114+
# Write your MySQL query statement below
115+
SELECT 'Low Salary' AS category, IFNULL(SUM(income < 20000), 0) AS accounts_count FROM Accounts
116+
UNION
117+
SELECT
118+
'Average Salary' AS category,
119+
IFNULL(SUM(income BETWEEN 20000 AND 50000), 0) AS accounts_count
120+
FROM Accounts
121+
UNION
122+
SELECT 'High Salary' AS category, IFNULL(SUM(income > 50000), 0) AS accounts_count FROM Accounts;
103123
```
104124

105125
<!-- tabs:end -->

solution/1900-1999/1907.Count Salary Categories/README_EN.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ High Salary: Accounts 3, 6, and 8.
6363

6464
## Solutions
6565

66+
**Solution 1: Temporary Table + Grouping + Left Join**
67+
68+
We can first create a temporary table containing all salary categories, and then count the number of bank accounts for each salary category. Finally, we use a left join to connect the temporary table with the result table to ensure that the result table contains all salary categories.
69+
70+
**Solution 2: Filtering + Merging**
71+
72+
We can filter out the number of bank accounts for each salary category separately, and then merge the results. Here, we use `UNION` to merge the results.
73+
6674
<!-- tabs:start -->
6775

6876
### **SQL**
@@ -86,12 +94,24 @@ WITH
8694
END AS category,
8795
COUNT(1) AS accounts_count
8896
FROM Accounts
89-
GROUP BY category
97+
GROUP BY 1
9098
)
91-
SELECT s.category, IFNULL(accounts_count, 0) AS accounts_count
99+
SELECT category, IFNULL(accounts_count, 0) AS accounts_count
92100
FROM
93-
S AS s
94-
LEFT JOIN T AS t ON s.category = t.category;
101+
S
102+
LEFT JOIN T USING (category);
103+
```
104+
105+
```sql
106+
# Write your MySQL query statement below
107+
SELECT 'Low Salary' AS category, IFNULL(SUM(income < 20000), 0) AS accounts_count FROM Accounts
108+
UNION
109+
SELECT
110+
'Average Salary' AS category,
111+
IFNULL(SUM(income BETWEEN 20000 AND 50000), 0) AS accounts_count
112+
FROM Accounts
113+
UNION
114+
SELECT 'High Salary' AS category, IFNULL(SUM(income > 50000), 0) AS accounts_count FROM Accounts;
95115
```
96116

97117
<!-- tabs:end -->

solution/1900-1999/1907.Count Salary Categories/Solution.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ WITH
1616
END AS category,
1717
COUNT(1) AS accounts_count
1818
FROM Accounts
19-
GROUP BY category
19+
GROUP BY 1
2020
)
21-
SELECT s.category, IFNULL(accounts_count, 0) AS accounts_count
21+
SELECT category, IFNULL(accounts_count, 0) AS accounts_count
2222
FROM
23-
S AS s
24-
LEFT JOIN T AS t ON s.category = t.category;
23+
S
24+
LEFT JOIN T USING (category);

solution/1900-1999/1978.Employees Whose Manager Left the Company/README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ Joziah 的上级经理是 6 号员工,他已经离职,因为员工表里面
6464

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

67+
**方法一:左连接**
68+
69+
我们可以使用左连接,将员工表自身连接一次,然后筛选出薪水小于 30000 的员工,且有上级经理,但是上级经理已经离职的员工。
70+
71+
**方法二:子查询**
72+
73+
我们也可以使用子查询,先找出所有已经离职的经理,然后再找出薪水小于 30000 的员工,且他们的上级经理不在已经离职的经理列表中。
74+
6775
<!-- tabs:start -->
6876

6977
### **SQL**
@@ -72,20 +80,20 @@ Joziah 的上级经理是 6 号员工,他已经离职,因为员工表里面
7280

7381
```sql
7482
# Write your MySQL query statement below
75-
SELECT a.employee_id
83+
SELECT e1.employee_id
7684
FROM
77-
Employees AS a
78-
LEFT JOIN Employees AS b ON a.manager_id = b.employee_id
79-
WHERE b.employee_id IS NULL AND a.salary < 30000 AND a.manager_id IS NOT NULL
80-
ORDER BY a.employee_id;
85+
Employees AS e1
86+
LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
87+
WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL
88+
ORDER BY 1;
8189
```
8290

8391
```sql
8492
# Write your MySQL query statement below
8593
SELECT employee_id
8694
FROM Employees
8795
WHERE salary < 30000 AND manager_id NOT IN (SELECT employee_id FROM Employees)
88-
ORDER BY employee_id;
96+
ORDER BY 1;
8997
```
9098

9199
<!-- tabs:end -->

solution/1900-1999/1978.Employees Whose Manager Left the Company/README_EN.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,34 @@ Joziah&#39;s manager is employee 6, who left the company because there is no row
5858

5959
## Solutions
6060

61+
**Solution 1: Left Join**
62+
63+
We can use a left join to connect the employee table with itself, and then filter out the employees whose salary is less than $30000$ and have a superior manager who has left the company.
64+
65+
**Solution 2: Subquery**
66+
67+
We can also use a subquery to first find all the managers who have left the company, and then find the employees whose salary is less than $30000$ and whose superior manager is not in the list of managers who have left the company.
68+
6169
<!-- tabs:start -->
6270

6371
### **SQL**
6472

6573
```sql
6674
# Write your MySQL query statement below
67-
SELECT a.employee_id
75+
SELECT e1.employee_id
6876
FROM
69-
Employees AS a
70-
LEFT JOIN Employees AS b ON a.manager_id = b.employee_id
71-
WHERE b.employee_id IS NULL AND a.salary < 30000 AND a.manager_id IS NOT NULL
72-
ORDER BY a.employee_id;
77+
Employees AS e1
78+
LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
79+
WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL
80+
ORDER BY 1;
7381
```
7482

7583
```sql
7684
# Write your MySQL query statement below
7785
SELECT employee_id
7886
FROM Employees
7987
WHERE salary < 30000 AND manager_id NOT IN (SELECT employee_id FROM Employees)
80-
ORDER BY employee_id;
88+
ORDER BY 1;
8189
```
8290

8391
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Write your MySQL query statement below
2-
SELECT a.employee_id
2+
SELECT e1.employee_id
33
FROM
4-
Employees AS a
5-
LEFT JOIN Employees AS b ON a.manager_id = b.employee_id
6-
WHERE b.employee_id IS NULL AND a.salary < 30000 AND a.manager_id IS NOT NULL
7-
ORDER BY a.employee_id;
4+
Employees AS e1
5+
LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
6+
WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL
7+
ORDER BY 1;

0 commit comments

Comments
 (0)