Skip to content

Commit 5591807

Browse files
authoredOct 12, 2023
feat: update sql solutions to lc problems (#1793)
* No.0512.Game Play Analysis II * No.1303.Find the Team Size * No.1350.Students With Invalid Departments * No.1747.Leetflex Banned Accounts * No.1783.Grand Slam Titles
1 parent 523a9cb commit 5591807

File tree

13 files changed

+170
-82
lines changed

13 files changed

+170
-82
lines changed
 

‎solution/0500-0599/0512.Game Play Analysis II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ WHERE
7979
player_id,
8080
min(event_date) AS event_date
8181
FROM Activity
82-
GROUP BY player_id
82+
GROUP BY 1
8383
);
8484
```
8585

‎solution/0500-0599/0512.Game Play Analysis II/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Activity table:
5555

5656
## Solutions
5757

58+
**Solution 1: Subquery**
59+
60+
We can use `GROUP BY` and `MIN` functions to find the first login date for each player, and then use a subquery with a composite key to find the first login device for each player.
61+
62+
**Solution 2: Window Function**
63+
64+
We can use the window function `rank()`, which assigns a rank to each login date for each player, and then select the rows with a rank of $1$.
65+
5866
<!-- tabs:start -->
5967

6068
### **SQL**
@@ -71,7 +79,7 @@ WHERE
7179
player_id,
7280
min(event_date) AS event_date
7381
FROM Activity
74-
GROUP BY player_id
82+
GROUP BY 1
7583
);
7684
```
7785

‎solution/0500-0599/0512.Game Play Analysis II/Solution.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ WHERE
99
player_id,
1010
min(event_date) AS event_date
1111
FROM Activity
12-
GROUP BY player_id
12+
GROUP BY 1
1313
);

‎solution/1300-1399/1303.Find the Team Size/README.md

+20-21
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,39 @@ ID 为 5、6 的员工是 team_id 为 9 的团队的成员。
6565

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

68+
**方法一:分组统计 + 等值连接**
69+
70+
我们可以先统计出每个团队的人数,记录在 `T` 表中,然后我们将 `Employee` 表与 `T` 表按照 `team_id` 进行等值连接,即可得到每个员工所在团队的总人数。
71+
72+
**方法二:左连接**
73+
74+
我们也可以使用左连接,将 `Employee` 表按照 `team_id` 进行自连接,然后按照 `employee_id` 进行分组,统计每个员工所在团队的总人数。
75+
6876
<!-- tabs:start -->
6977

7078
### **SQL**
7179

72-
解法 1:
73-
7480
```sql
7581
# Write your MySQL query statement below
76-
SELECT
77-
e.employee_id,
78-
t.team_size
79-
FROM
80-
Employee AS e
81-
LEFT JOIN (
82-
SELECT
83-
team_id,
84-
count(1) AS team_size
82+
WITH
83+
T AS (
84+
SELECT team_id, count(1) AS team_size
8585
FROM Employee
86-
GROUP BY team_id
87-
) AS t
88-
ON e.team_id = t.team_id;
86+
GROUP BY 1
87+
)
88+
SELECT employee_id, team_size
89+
FROM
90+
Employee
91+
JOIN T USING (team_id);
8992
```
9093

91-
解法 2:
92-
9394
```sql
9495
# Write your MySQL query statement below
95-
SELECT
96-
e1.employee_id,
97-
count(*) AS team_size
96+
SELECT e1.employee_id, count(1) AS team_size
9897
FROM
9998
Employee AS e1
100-
LEFT JOIN Employee AS e2 ON e1.team_id = e2.team_id
101-
GROUP BY e1.employee_id;
99+
LEFT JOIN Employee AS e2 USING (team_id)
100+
GROUP BY 1;
102101
```
103102

104103
<!-- tabs:end -->

‎solution/1300-1399/1303.Find the Team Size/README_EN.md

+20-21
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,39 @@ Employees with Id 5,6 are part of a team with team_id = 9.
6060

6161
## Solutions
6262

63+
**Solution 1: Group By + Equi-Join**
64+
65+
We can first count the number of people in each team and record it in the `T` table. Then, we can use an equi-join to join the `Employee` table and the `T` table based on `team_id`, and obtain the total number of people in each team.
66+
67+
**Solution 2: Left Join**
68+
69+
We can also use a left join to join the `Employee` table with itself based on `team_id`, and then group by `employee_id` to count the total number of people in each team that the employee belongs to.
70+
6371
<!-- tabs:start -->
6472

6573
### **SQL**
6674

67-
Solution 1:
68-
6975
```sql
7076
# Write your MySQL query statement below
71-
SELECT
72-
e.employee_id,
73-
t.team_size
74-
FROM
75-
Employee AS e
76-
LEFT JOIN (
77-
SELECT
78-
team_id,
79-
count(1) AS team_size
77+
WITH
78+
T AS (
79+
SELECT team_id, count(1) AS team_size
8080
FROM Employee
81-
GROUP BY team_id
82-
) AS t
83-
ON e.team_id = t.team_id;
81+
GROUP BY 1
82+
)
83+
SELECT employee_id, team_size
84+
FROM
85+
Employee
86+
JOIN T USING (team_id);
8487
```
8588

86-
Solution 2:
87-
8889
```sql
8990
# Write your MySQL query statement below
90-
SELECT
91-
e1.employee_id,
92-
count(*) AS team_size
91+
SELECT e1.employee_id, count(1) AS team_size
9392
FROM
9493
Employee AS e1
95-
LEFT JOIN Employee AS e2 ON e1.team_id = e2.team_id
96-
GROUP BY e1.employee_id;
94+
LEFT JOIN Employee AS e2 USING (team_id)
95+
GROUP BY 1;
9796
```
9897

9998
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
e1.employee_id,
4-
count(*) AS team_size
2+
SELECT e1.employee_id, count(1) AS team_size
53
FROM
64
Employee AS e1
7-
LEFT JOIN Employee AS e2 ON e1.team_id = e2.team_id
8-
GROUP BY e1.employee_id;
5+
LEFT JOIN Employee AS e2 USING (team_id)
6+
GROUP BY 1;

‎solution/1300-1399/1350.Students With Invalid Departments/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,25 @@ John, Daiana, Steve 和 Jasmine 所在的院系分别是 14, 33, 74 和 77,
8888

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

91-
**方法一:左连接**
91+
**方法一:子查询**
9292

93-
我们将 `Students` 表左连接 `Departments` 表,然后筛选出所有 `Departments` 表中 `id``NULL` 的记录即可。
93+
我们直接使用子查询的方式,找出所有不在院系表中的学生即可。
94+
95+
**方法二:左连接**
96+
97+
我们也可以使用左连接,将 `Students` 表和 `Departments` 连接,连接条件为 `Students.department_id = Departments.id`,然后筛选出 `Departments.id` 为空的学生即可。
9498

9599
<!-- tabs:start -->
96100

97101
### **SQL**
98102

103+
```sql
104+
# Write your MySQL query statement below
105+
SELECT id, name
106+
FROM Students
107+
WHERE department_id NOT IN (SELECT id FROM Departments);
108+
```
109+
99110
```sql
100111
# Write your MySQL query statement below
101112
SELECT s.id, s.name

‎solution/1300-1399/1350.Students With Invalid Departments/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,25 @@ John, Daiana, Steve, and Jasmine are enrolled in departments 14, 33, 74, and 77
8484

8585
## Solutions
8686

87+
**Solution 1: Subquery**
88+
89+
We can directly use a subquery to find all students who are not in the `Departments` table.
90+
91+
**Solution 2: Left Join**
92+
93+
We can also use a left join to join the `Students` table with the `Departments` table on the condition of `Students.department_id = Departments.id`, and then filter out the students whose `Departments.id` is `NULL`.
94+
8795
<!-- tabs:start -->
8896

8997
### **SQL**
9098

99+
```sql
100+
# Write your MySQL query statement below
101+
SELECT id, name
102+
FROM Students
103+
WHERE department_id NOT IN (SELECT id FROM Departments);
104+
```
105+
91106
```sql
92107
# Write your MySQL query statement below
93108
SELECT s.id, s.name

‎solution/1700-1799/1747.Leetflex Banned Accounts/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ Account ID 4 --&gt; 该账户从 "2021-02-01 17:00:00" 到 "2021-02-01 17:00:00"
6666

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

69+
**方法一:自连接**
70+
71+
我们可以通过自连接的方式,找出每个账户在同一天内,从不同的网络地址登录的情况。连接的条件是:
72+
73+
- 账户编号相同
74+
- 网络地址不同
75+
- 一次登录的时间在另一次“登录-退出”的时间范围内
76+
6977
<!-- tabs:start -->
7078

7179
### **SQL**

‎solution/1700-1799/1747.Leetflex Banned Accounts/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ Account ID 4 --&gt; The account was active from &quot;2021-02-01 17:00:00&quot;
6262

6363
## Solutions
6464

65+
**Solution 1: Self-Join**
66+
67+
We can use a self-join to find out the cases where each account logs in from different IP addresses on the same day. The conditions for joining are:
68+
69+
- The account numbers are the same.
70+
- The IP addresses are different.
71+
- The login time of one record is within the login-logout time range of another record.
72+
6573
<!-- tabs:start -->
6674

6775
### **SQL**

‎solution/1700-1799/1783.Grand Slam Titles/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,37 @@ Player 3 (Novak) 没有赢得,因此不包含在结果集中。</pre>
8383

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

86+
**方法一:合并 + 等值连接 + 分组**
87+
88+
我们可以使用 `UNION ALL`,将所有赢得大满贯比赛的球员 ID 合并到一张表 `T` 中,然后使用等值连接 `JOIN`,将 `T` 表与 `Players` 表按照 `player_id` 进行连接,最后使用 `GROUP BY``COUNT` 统计每个球员赢得大满贯比赛的次数。
89+
8690
<!-- tabs:start -->
8791

8892
### **SQL**
8993

94+
```sql
95+
# Write your MySQL query statement below
96+
WITH
97+
T AS (
98+
SELECT Wimbledon AS player_id
99+
FROM Championships
100+
UNION ALL
101+
SELECT Fr_open AS player_id
102+
FROM Championships
103+
UNION ALL
104+
SELECT US_open AS player_id
105+
FROM Championships
106+
UNION ALL
107+
SELECT Au_open AS player_id
108+
FROM Championships
109+
)
110+
SELECT player_id, player_name, count(1) AS grand_slams_count
111+
FROM
112+
T
113+
JOIN Players USING (player_id)
114+
GROUP BY 1;
115+
```
116+
90117
```sql
91118
# Write your MySQL query statement below
92119
SELECT

‎solution/1700-1799/1783.Grand Slam Titles/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,37 @@ Player 3 (Novak) did not win anything, we did not include them in the result tab
7979

8080
## Solutions
8181

82+
**Solution 1: Union All + Equi-Join + Group By**
83+
84+
We can use `UNION ALL` to merge all player IDs who won Grand Slam titles into a table `T`, then use an equi-join `JOIN` to join `T` table with `Players` table on `player_id`, and finally use `GROUP BY` and `COUNT` to count the number of Grand Slam titles won by each player.
85+
8286
<!-- tabs:start -->
8387

8488
### **SQL**
8589

90+
```sql
91+
# Write your MySQL query statement below
92+
WITH
93+
T AS (
94+
SELECT Wimbledon AS player_id
95+
FROM Championships
96+
UNION ALL
97+
SELECT Fr_open AS player_id
98+
FROM Championships
99+
UNION ALL
100+
SELECT US_open AS player_id
101+
FROM Championships
102+
UNION ALL
103+
SELECT Au_open AS player_id
104+
FROM Championships
105+
)
106+
SELECT player_id, player_name, count(1) AS grand_slams_count
107+
FROM
108+
T
109+
JOIN Players USING (player_id)
110+
GROUP BY 1;
111+
```
112+
86113
```sql
87114
# Write your MySQL query statement below
88115
SELECT
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
player_id,
4-
player_name,
5-
sum(
6-
(
7-
CASE
8-
WHEN Wimbledon = player_id THEN 1
9-
ELSE 0
10-
END
11-
) + (
12-
CASE
13-
WHEN Fr_open = player_id THEN 1
14-
ELSE 0
15-
END
16-
) + (
17-
CASE
18-
WHEN US_open = player_id THEN 1
19-
ELSE 0
20-
END
21-
) + (
22-
CASE
23-
WHEN Au_open = player_id THEN 1
24-
ELSE 0
25-
END
26-
)
27-
) AS grand_slams_count
2+
WITH
3+
T AS (
4+
SELECT Wimbledon AS player_id
5+
FROM Championships
6+
UNION ALL
7+
SELECT Fr_open AS player_id
8+
FROM Championships
9+
UNION ALL
10+
SELECT US_open AS player_id
11+
FROM Championships
12+
UNION ALL
13+
SELECT Au_open AS player_id
14+
FROM Championships
15+
)
16+
SELECT player_id, player_name, count(1) AS grand_slams_count
2817
FROM
29-
Championships
30-
CROSS JOIN Players
31-
GROUP BY player_id
32-
HAVING grand_slams_count > 0;
18+
T
19+
JOIN Players USING (player_id)
20+
GROUP BY 1;

0 commit comments

Comments
 (0)