Skip to content

Commit 49ee3ff

Browse files
authored
feat: update solutions to lc problems: No.0570,0620,1251,1280,1934 (doocs#1834)
* No.0570.Managers with at Least 5 Direct Reports * No.0620.Not Boring Movies * No.1251.Average Selling Price * No.1280.Students and Examinations * No.1934.Confirmation Rate
1 parent c306004 commit 49ee3ff

File tree

15 files changed

+105
-120
lines changed

15 files changed

+105
-120
lines changed

solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md

+11-26
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,26 @@ Employee 表:
5959

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

62+
**方法一:分组统计 + 连接**
63+
64+
我们可以先统计每个经理的直接下属人数,然后再连接 `Employee` 表,找出直接下属人数大于等于 $5$ 的经理。
65+
6266
<!-- tabs:start -->
6367

6468
### **SQL**
6569

6670
```sql
6771
# Write your MySQL query statement below
68-
SELECT
69-
name
72+
SELECT name
7073
FROM
71-
Employee AS e1
74+
Employee
7275
JOIN (
73-
SELECT
74-
managerId
75-
FROM Employee
76-
WHERE managerId IS NOT NULL
77-
GROUP BY managerId
78-
HAVING COUNT(1) >= 5
79-
) AS e2
80-
ON e1.id = e2.managerId;
81-
```
82-
83-
```sql
84-
# Write your MySQL query statement below
85-
WITH
86-
T AS (
87-
SELECT
88-
managerId,
89-
COUNT(1) OVER (PARTITION BY managerId) AS cnt
76+
SELECT managerId AS id, COUNT(1) AS cnt
9077
FROM Employee
91-
)
92-
SELECT DISTINCT name
93-
FROM
94-
Employee AS e
95-
JOIN T AS t ON e.id = t.managerId
96-
WHERE cnt >= 5;
78+
GROUP BY 1
79+
HAVING cnt >= 5
80+
) AS t
81+
USING (id);
9782
```
9883

9984
<!-- tabs:end -->

solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md

+11-26
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,26 @@ Employee table:
5555

5656
## Solutions
5757

58+
**Solution 1: Grouping and Joining**
59+
60+
We can first count the number of direct subordinates for each manager, and then join the `Employee` table to find the managers whose number of direct subordinates is greater than or equal to $5$.
61+
5862
<!-- tabs:start -->
5963

6064
### **SQL**
6165

6266
```sql
6367
# Write your MySQL query statement below
64-
SELECT
65-
name
68+
SELECT name
6669
FROM
67-
Employee AS e1
70+
Employee
6871
JOIN (
69-
SELECT
70-
managerId
71-
FROM Employee
72-
WHERE managerId IS NOT NULL
73-
GROUP BY managerId
74-
HAVING COUNT(1) >= 5
75-
) AS e2
76-
ON e1.id = e2.managerId;
77-
```
78-
79-
```sql
80-
# Write your MySQL query statement below
81-
WITH
82-
T AS (
83-
SELECT
84-
managerId,
85-
COUNT(1) OVER (PARTITION BY managerId) AS cnt
72+
SELECT managerId AS id, COUNT(1) AS cnt
8673
FROM Employee
87-
)
88-
SELECT DISTINCT name
89-
FROM
90-
Employee AS e
91-
JOIN T AS t ON e.id = t.managerId
92-
WHERE cnt >= 5;
74+
GROUP BY 1
75+
HAVING cnt >= 5
76+
) AS t
77+
USING (id);
9378
```
9479

9580
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Write your MySQL query statement below
2-
WITH
3-
T AS (
4-
SELECT
5-
managerId,
6-
COUNT(1) OVER (PARTITION BY managerId) AS cnt
7-
FROM Employee
8-
)
9-
SELECT DISTINCT name
2+
SELECT name
103
FROM
11-
Employee AS e
12-
JOIN T AS t ON e.id = t.managerId
13-
WHERE cnt >= 5;
4+
Employee
5+
JOIN (
6+
SELECT managerId AS id, COUNT(1) AS cnt
7+
FROM Employee
8+
GROUP BY 1
9+
HAVING cnt >= 5
10+
) AS t
11+
USING (id);

solution/0600-0699/0620.Not Boring Movies/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ id 是该表的主键(具有唯一值的列)。
6060

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

63+
**方法一:条件筛选 + 排序**
64+
65+
我们可以使用 `WHERE` 子句筛选出 `description` 不为 `boring`,并且 `id` 为奇数的记录,然后使用 `ORDER BY` 子句对结果按照 `rating` 降序排序。
66+
6367
<!-- tabs:start -->
6468

6569
### **SQL**
@@ -68,8 +72,8 @@ id 是该表的主键(具有唯一值的列)。
6872
# Write your MySQL query statement below
6973
SELECT *
7074
FROM Cinema
71-
WHERE description != 'boring' AND id % 2 = 1
72-
ORDER BY rating DESC;
75+
WHERE description != 'boring' AND id & 1 = 1
76+
ORDER BY 4 DESC;
7377
```
7478

7579
<!-- tabs:end -->

solution/0600-0699/0620.Not Boring Movies/README_EN.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 i
5656

5757
## Solutions
5858

59+
**Solution 1: Conditional Filtering + Sorting**
60+
61+
We can use the `WHERE` clause to filter out the records where `description` is not `boring` and `id` is odd, and then use the `ORDER BY` clause to sort the result in descending order by `rating`.
62+
5963
<!-- tabs:start -->
6064

6165
### **SQL**
@@ -64,8 +68,8 @@ We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 i
6468
# Write your MySQL query statement below
6569
SELECT *
6670
FROM Cinema
67-
WHERE description != 'boring' AND id % 2 = 1
68-
ORDER BY rating DESC;
71+
WHERE description != 'boring' AND id & 1 = 1
72+
ORDER BY 4 DESC;
6973
```
7074

7175
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
22
SELECT *
33
FROM Cinema
4-
WHERE description != 'boring' AND id % 2 = 1
5-
ORDER BY rating DESC;
4+
WHERE description != 'boring' AND id & 1 = 1
5+
ORDER BY 4 DESC;

solution/1200-1299/1251.Average Selling Price/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,24 @@ UnitsSold table:
8484

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

87+
**方法一:左连接 + 分组统计**
88+
89+
我们可以使用左连接,将 `Prices` 表和 `UnitsSold` 表连接起来,连接条件为 `product_id` 相等,并且 `purchase_date``start_date``end_date` 之间。然后使用 `GROUP BY` 子句对 `product_id` 进行分组,使用 `AVG` 函数计算平均价格。注意,如果某个产品没有销售记录,那么 `AVG` 函数会返回 `NULL`,因此我们可以使用 `IFNULL` 函数将其转换为 $0$。
90+
8791
<!-- tabs:start -->
8892

8993
### **SQL**
9094

9195
```sql
96+
# Write your MySQL query statement below
9297
SELECT
9398
p.product_id,
94-
IFNULL(ROUND(SUM(units * price) / SUM(units), 2), 0) AS average_price
99+
IFNULL(ROUND(SUM(price * units) / SUM(units), 2), 0) AS average_price
95100
FROM
96101
Prices AS p
97102
LEFT JOIN UnitsSold AS u
98103
ON p.product_id = u.product_id AND purchase_date BETWEEN start_date AND end_date
99-
GROUP BY product_id;
104+
GROUP BY 1;
100105
```
101106

102107
<!-- tabs:end -->

solution/1200-1299/1251.Average Selling Price/README_EN.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,24 @@ Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
8282

8383
## Solutions
8484

85+
**Solution 1: Left Join + Grouping**
86+
87+
We can use a left join to join the `Prices` table and the `UnitsSold` table on `product_id`, and the condition that `purchase_date` is between `start_date` and `end_date`. Then, we can use `GROUP BY` to group by `product_id` for aggregation, and use the `AVG` function to calculate the average price. Note that if a product has no sales records, the `AVG` function will return `NULL`, so we can use the `IFNULL` function to convert it to $0$.
88+
8589
<!-- tabs:start -->
8690

8791
### **SQL**
8892

8993
```sql
94+
# Write your MySQL query statement below
9095
SELECT
9196
p.product_id,
92-
IFNULL(ROUND(SUM(units * price) / SUM(units), 2), 0) AS average_price
97+
IFNULL(ROUND(SUM(price * units) / SUM(units), 2), 0) AS average_price
9398
FROM
9499
Prices AS p
95100
LEFT JOIN UnitsSold AS u
96101
ON p.product_id = u.product_id AND purchase_date BETWEEN start_date AND end_date
97-
GROUP BY product_id;
102+
GROUP BY 1;
98103
```
99104

100105
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
# Write your MySQL query statement below
12
SELECT
23
p.product_id,
3-
IFNULL(ROUND(SUM(units * price) / SUM(units), 2), 0) AS average_price
4+
IFNULL(ROUND(SUM(price * units) / SUM(units), 2), 0) AS average_price
45
FROM
56
Prices AS p
67
LEFT JOIN UnitsSold AS u
78
ON p.product_id = u.product_id AND purchase_date BETWEEN start_date AND end_date
8-
GROUP BY product_id;
9+
GROUP BY 1;

solution/1200-1299/1280.Students and Examinations/README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,23 @@ John 参加了数学、物理、编程测试各 1 次。
123123

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

126+
**方法一:两次连接 + 分组统计**
127+
128+
我们可以先连接 `Students` 表和 `Subjects` 表,得到所有学生和所有科目的组合,然后再连接 `Examinations` 表,连接条件为 `student_id``subject_name`,这样就得到了每个学生参加每一门科目测试的次数,最后按 `student_id``subject_name` 分组统计即可。
129+
126130
<!-- tabs:start -->
127131

128132
### **SQL**
129133

130134
```sql
131135
# Write your MySQL query statement below
132-
SELECT
133-
a.student_id,
134-
student_name,
135-
b.subject_name,
136-
COUNT(c.subject_name) AS attended_exams
136+
SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
137137
FROM
138-
Students AS a
139-
CROSS JOIN Subjects AS b
140-
LEFT JOIN Examinations AS c ON a.student_id = c.student_id AND b.subject_name = c.subject_name
141-
GROUP BY a.student_id, b.subject_name
142-
ORDER BY a.student_id, b.subject_name;
138+
Students
139+
JOIN Subjects
140+
LEFT JOIN Examinations AS e USING (student_id, subject_name)
141+
GROUP BY 1, 3
142+
ORDER BY 1, 3;
143143
```
144144

145145
<!-- tabs:end -->

solution/1200-1299/1280.Students and Examinations/README_EN.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,23 @@ John attended the Math exam 1 time, the Physics exam 1 time, and the Programming
120120

121121
## Solutions
122122

123+
**Solution 1: Two Joins + Grouping**
124+
125+
We can first join the `Students` table and the `Subjects` table to obtain all combinations of students and subjects, and then join the `Examinations` table with the condition of `student_id` and `subject_name`. This way, we can get the number of times each student has taken each subject's test. Finally, we can group by `student_id` and `subject_name` to count the number of times each student has taken each subject's test.
126+
123127
<!-- tabs:start -->
124128

125129
### **SQL**
126130

127131
```sql
128132
# Write your MySQL query statement below
129-
SELECT
130-
a.student_id,
131-
student_name,
132-
b.subject_name,
133-
COUNT(c.subject_name) AS attended_exams
133+
SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
134134
FROM
135-
Students AS a
136-
CROSS JOIN Subjects AS b
137-
LEFT JOIN Examinations AS c ON a.student_id = c.student_id AND b.subject_name = c.subject_name
138-
GROUP BY a.student_id, b.subject_name
139-
ORDER BY a.student_id, b.subject_name;
135+
Students
136+
JOIN Subjects
137+
LEFT JOIN Examinations AS e USING (student_id, subject_name)
138+
GROUP BY 1, 3
139+
ORDER BY 1, 3;
140140
```
141141

142142
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
a.student_id,
4-
student_name,
5-
b.subject_name,
6-
COUNT(c.subject_name) AS attended_exams
2+
SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
73
FROM
8-
Students AS a
9-
CROSS JOIN Subjects AS b
10-
LEFT JOIN Examinations AS c ON a.student_id = c.student_id AND b.subject_name = c.subject_name
11-
GROUP BY a.student_id, b.subject_name
12-
ORDER BY a.student_id, b.subject_name;
4+
Students
5+
JOIN Subjects
6+
LEFT JOIN Examinations AS e USING (student_id, subject_name)
7+
GROUP BY 1, 3
8+
ORDER BY 1, 3;

solution/1900-1999/1934.Confirmation Rate/README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Confirmations 表:
9191

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

94+
**方法一:左连接 + 分组统计**
95+
96+
我们可以使用左连接,将 `Signups` 表和 `Confirmations` 表按照 `user_id` 进行连接,然后使用 `GROUP BY``user_id` 进行分组统计。
97+
9498
<!-- tabs:start -->
9599

96100
### **SQL**
@@ -101,14 +105,11 @@ Confirmations 表:
101105
# Write your MySQL query statement below
102106
SELECT
103107
user_id,
104-
ROUND(
105-
SUM(IF(action = 'confirmed', 1, 0)) / COUNT(1),
106-
2
107-
) AS confirmation_rate
108+
ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
108109
FROM
109-
Signups
110+
SignUps
110111
LEFT JOIN Confirmations USING (user_id)
111-
GROUP BY user_id;
112+
GROUP BY 1;
112113
```
113114

114115
<!-- tabs:end -->

solution/1900-1999/1934.Confirmation Rate/README_EN.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ User 2 made 2 requests where one was confirmed and the other timed out. The conf
8989

9090
## Solutions
9191

92+
**Solution 1: Left Join + Grouping**
93+
94+
We can use a left join to join the `Signups` table and the `Confirmations` table on `user_id`, and then use `GROUP BY` to group by `user_id` for aggregation.
95+
9296
<!-- tabs:start -->
9397

9498
### **SQL**
@@ -97,14 +101,11 @@ User 2 made 2 requests where one was confirmed and the other timed out. The conf
97101
# Write your MySQL query statement below
98102
SELECT
99103
user_id,
100-
ROUND(
101-
SUM(IF(action = 'confirmed', 1, 0)) / COUNT(1),
102-
2
103-
) AS confirmation_rate
104+
ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
104105
FROM
105-
Signups
106+
SignUps
106107
LEFT JOIN Confirmations USING (user_id)
107-
GROUP BY user_id;
108+
GROUP BY 1;
108109
```
109110

110111
<!-- tabs:end -->

0 commit comments

Comments
 (0)