diff --git a/solution/1900-1999/1907.Count Salary Categories/README.md b/solution/1900-1999/1907.Count Salary Categories/README.md index 4b2fa10c5a423..0635edcc6aea6 100644 --- a/solution/1900-1999/1907.Count Salary Categories/README.md +++ b/solution/1900-1999/1907.Count Salary Categories/README.md @@ -69,6 +69,14 @@ Accounts 表: +**方法一:构建临时表 + 分组统计 + 左连接** + +我们可以先构建一个临时表,包含所有工资类别,然后再统计每个工资类别的银行账户数量。最后我们使用左连接,将临时表和统计结果表连接起来,这样就可以保证结果表中包含所有工资类别。 + +**方法二:筛选 + 合并** + +我们可以分别筛选出每个工资类别的银行账户数量,然后再将结果合并起来。这里我们使用 `UNION` 来合并结果。 + ### **SQL** @@ -94,12 +102,24 @@ WITH END AS category, COUNT(1) AS accounts_count FROM Accounts - GROUP BY category + GROUP BY 1 ) -SELECT s.category, IFNULL(accounts_count, 0) AS accounts_count +SELECT category, IFNULL(accounts_count, 0) AS accounts_count FROM - S AS s - LEFT JOIN T AS t ON s.category = t.category; + S + LEFT JOIN T USING (category); +``` + +```sql +# Write your MySQL query statement below +SELECT 'Low Salary' AS category, IFNULL(SUM(income < 20000), 0) AS accounts_count FROM Accounts +UNION +SELECT + 'Average Salary' AS category, + IFNULL(SUM(income BETWEEN 20000 AND 50000), 0) AS accounts_count +FROM Accounts +UNION +SELECT 'High Salary' AS category, IFNULL(SUM(income > 50000), 0) AS accounts_count FROM Accounts; ``` diff --git a/solution/1900-1999/1907.Count Salary Categories/README_EN.md b/solution/1900-1999/1907.Count Salary Categories/README_EN.md index 9d193d73efb72..c7fd32769dacc 100644 --- a/solution/1900-1999/1907.Count Salary Categories/README_EN.md +++ b/solution/1900-1999/1907.Count Salary Categories/README_EN.md @@ -63,6 +63,14 @@ High Salary: Accounts 3, 6, and 8. ## Solutions +**Solution 1: Temporary Table + Grouping + Left Join** + +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. + +**Solution 2: Filtering + Merging** + +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. + ### **SQL** @@ -86,12 +94,24 @@ WITH END AS category, COUNT(1) AS accounts_count FROM Accounts - GROUP BY category + GROUP BY 1 ) -SELECT s.category, IFNULL(accounts_count, 0) AS accounts_count +SELECT category, IFNULL(accounts_count, 0) AS accounts_count FROM - S AS s - LEFT JOIN T AS t ON s.category = t.category; + S + LEFT JOIN T USING (category); +``` + +```sql +# Write your MySQL query statement below +SELECT 'Low Salary' AS category, IFNULL(SUM(income < 20000), 0) AS accounts_count FROM Accounts +UNION +SELECT + 'Average Salary' AS category, + IFNULL(SUM(income BETWEEN 20000 AND 50000), 0) AS accounts_count +FROM Accounts +UNION +SELECT 'High Salary' AS category, IFNULL(SUM(income > 50000), 0) AS accounts_count FROM Accounts; ``` diff --git a/solution/1900-1999/1907.Count Salary Categories/Solution.sql b/solution/1900-1999/1907.Count Salary Categories/Solution.sql index f6a6acfa56042..c00acc4ad53b4 100644 --- a/solution/1900-1999/1907.Count Salary Categories/Solution.sql +++ b/solution/1900-1999/1907.Count Salary Categories/Solution.sql @@ -16,9 +16,9 @@ WITH END AS category, COUNT(1) AS accounts_count FROM Accounts - GROUP BY category + GROUP BY 1 ) -SELECT s.category, IFNULL(accounts_count, 0) AS accounts_count +SELECT category, IFNULL(accounts_count, 0) AS accounts_count FROM - S AS s - LEFT JOIN T AS t ON s.category = t.category; + S + LEFT JOIN T USING (category); diff --git a/solution/1900-1999/1978.Employees Whose Manager Left the Company/README.md b/solution/1900-1999/1978.Employees Whose Manager Left the Company/README.md index 0e069884423f9..84fd4dfb30e1c 100644 --- a/solution/1900-1999/1978.Employees Whose Manager Left the Company/README.md +++ b/solution/1900-1999/1978.Employees Whose Manager Left the Company/README.md @@ -64,6 +64,14 @@ Joziah 的上级经理是 6 号员工,他已经离职,因为员工表里面 +**方法一:左连接** + +我们可以使用左连接,将员工表自身连接一次,然后筛选出薪水小于 30000 的员工,且有上级经理,但是上级经理已经离职的员工。 + +**方法二:子查询** + +我们也可以使用子查询,先找出所有已经离职的经理,然后再找出薪水小于 30000 的员工,且他们的上级经理不在已经离职的经理列表中。 + ### **SQL** @@ -72,12 +80,12 @@ Joziah 的上级经理是 6 号员工,他已经离职,因为员工表里面 ```sql # Write your MySQL query statement below -SELECT a.employee_id +SELECT e1.employee_id FROM - Employees AS a - LEFT JOIN Employees AS b ON a.manager_id = b.employee_id -WHERE b.employee_id IS NULL AND a.salary < 30000 AND a.manager_id IS NOT NULL -ORDER BY a.employee_id; + Employees AS e1 + LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id +WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL +ORDER BY 1; ``` ```sql @@ -85,7 +93,7 @@ ORDER BY a.employee_id; SELECT employee_id FROM Employees WHERE salary < 30000 AND manager_id NOT IN (SELECT employee_id FROM Employees) -ORDER BY employee_id; +ORDER BY 1; ``` diff --git a/solution/1900-1999/1978.Employees Whose Manager Left the Company/README_EN.md b/solution/1900-1999/1978.Employees Whose Manager Left the Company/README_EN.md index 2409a6cc8626a..4aaf8dd1e6ccb 100644 --- a/solution/1900-1999/1978.Employees Whose Manager Left the Company/README_EN.md +++ b/solution/1900-1999/1978.Employees Whose Manager Left the Company/README_EN.md @@ -58,18 +58,26 @@ Joziah's manager is employee 6, who left the company because there is no row ## Solutions +**Solution 1: Left Join** + +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. + +**Solution 2: Subquery** + +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. + ### **SQL** ```sql # Write your MySQL query statement below -SELECT a.employee_id +SELECT e1.employee_id FROM - Employees AS a - LEFT JOIN Employees AS b ON a.manager_id = b.employee_id -WHERE b.employee_id IS NULL AND a.salary < 30000 AND a.manager_id IS NOT NULL -ORDER BY a.employee_id; + Employees AS e1 + LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id +WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL +ORDER BY 1; ``` ```sql @@ -77,7 +85,7 @@ ORDER BY a.employee_id; SELECT employee_id FROM Employees WHERE salary < 30000 AND manager_id NOT IN (SELECT employee_id FROM Employees) -ORDER BY employee_id; +ORDER BY 1; ``` diff --git a/solution/1900-1999/1978.Employees Whose Manager Left the Company/Solution.sql b/solution/1900-1999/1978.Employees Whose Manager Left the Company/Solution.sql index e0e894d3b5812..041dfdf9f6b8a 100644 --- a/solution/1900-1999/1978.Employees Whose Manager Left the Company/Solution.sql +++ b/solution/1900-1999/1978.Employees Whose Manager Left the Company/Solution.sql @@ -1,7 +1,7 @@ # Write your MySQL query statement below -SELECT a.employee_id +SELECT e1.employee_id FROM - Employees AS a - LEFT JOIN Employees AS b ON a.manager_id = b.employee_id -WHERE b.employee_id IS NULL AND a.salary < 30000 AND a.manager_id IS NOT NULL -ORDER BY a.employee_id; + Employees AS e1 + LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id +WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL +ORDER BY 1;