Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5591807

Browse files
feat: update sql solutions to lc problems (doocs#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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 9 additions & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 20 additions & 21 deletions
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 ONe1.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‎

Lines changed: 20 additions & 21 deletions
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 ONe1.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 -->
Lines changed: 3 additions & 5 deletions
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 ONe1.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‎

Lines changed: 13 additions & 2 deletions
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‎

Lines changed: 15 additions & 0 deletions
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‎

Lines changed: 8 additions & 0 deletions
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‎

Lines changed: 8 additions & 0 deletions
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**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /