Skip to content

feat: update solutions to lc problems: No.1907,1978 #1878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions solution/1900-1999/1907.Count Salary Categories/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ Accounts 表:

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

**方法一:构建临时表 + 分组统计 + 左连接**

我们可以先构建一个临时表,包含所有工资类别,然后再统计每个工资类别的银行账户数量。最后我们使用左连接,将临时表和统计结果表连接起来,这样就可以保证结果表中包含所有工资类别。

**方法二:筛选 + 合并**

我们可以分别筛选出每个工资类别的银行账户数量,然后再将结果合并起来。这里我们使用 `UNION` 来合并结果。

<!-- tabs:start -->

### **SQL**
Expand All @@ -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;
```

<!-- tabs:end -->
28 changes: 24 additions & 4 deletions solution/1900-1999/1907.Count Salary Categories/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

### **SQL**
Expand All @@ -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;
```

<!-- tabs:end -->
8 changes: 4 additions & 4 deletions solution/1900-1999/1907.Count Salary Categories/Solution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ Joziah 的上级经理是 6 号员工,他已经离职,因为员工表里面

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

**方法一:左连接**

我们可以使用左连接,将员工表自身连接一次,然后筛选出薪水小于 30000 的员工,且有上级经理,但是上级经理已经离职的员工。

**方法二:子查询**

我们也可以使用子查询,先找出所有已经离职的经理,然后再找出薪水小于 30000 的员工,且他们的上级经理不在已经离职的经理列表中。

<!-- tabs:start -->

### **SQL**
Expand All @@ -72,20 +80,20 @@ 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
# Write your MySQL query statement below
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;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,34 @@ Joziah&#39;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.

<!-- tabs:start -->

### **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
# Write your MySQL query statement below
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;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -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;