Skip to content

Commit 94d498d

Browse files
authored
feat: update solutions to lc problems (#1788)
* No.0182.Duplicate Emails * No.1050.Actors and Directors Who Cooperated At Least Three Times * No.1495.Friendly Movies Streamed Last Month * No.1511.Customer Order Frequency * No.1587.Bank Account Summary II * No.1693.Daily Leads and Partners * No.1699.Number of Calls Between Two Persons * No.2562.Find the Array Concatenation Value
1 parent 451ca21 commit 94d498d

File tree

24 files changed

+191
-147
lines changed

24 files changed

+191
-147
lines changed

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ node_modules/
1616
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
1717
/solution/1400-1499/1454.Active Users/Solution.sql
1818
/solution/1400-1499/1484.Group Sold Products By The Date/Solution.sql
19-
/solution/1500-1599/1511.Customer Order Frequency/Solution.sql
2019
/solution/1600-1699/1613.Find the Missing IDs/Solution.sql
2120
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
2221
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql

.prettierrc

+2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
"solution/1400-1499/1445.Apples & Oranges/Solution.sql",
3535
"solution/1400-1499/1479.Sales by Day of the Week/Solution.sql",
3636
"solution/1500-1599/1501.Countries You Can Safely Invest In/Solution.sql",
37+
"solution/1500-1599/1511.Customer Order Frequency/Solution.sql",
3738
"solution/1500-1599/1555.Bank Account Summary/Solution.sql",
3839
"solution/1600-1699/1667.Fix Names in a Table/Solution.sql",
40+
"solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql",
3941
"solution/1700-1799/1777.Product's Price for Each Store/Solution.sql",
4042
"solution/1900-1999/1934.Confirmation Rate/Solution.sql",
4143
"solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql",

solution/0100-0199/0182.Duplicate Emails/README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,24 @@ Person 表:
5555

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

58+
**方法一:分组统计**
59+
60+
我们可以使用 `GROUP BY` 语句,按照 `email` 字段进行分组,然后使用 `HAVING` 语句,筛选出现次数大于 $1$ 的 `email`
61+
62+
**方法二:自连接**
63+
64+
我们可以使用自连接的方法,将 `Person` 表自身连接一次,然后筛选出 `id` 不同,但 `email` 相同的记录。
65+
5866
<!-- tabs:start -->
5967

6068
### **SQL**
6169

6270
```sql
63-
SELECT Email
71+
# Write your MySQL query statement below
72+
SELECT email
6473
FROM Person
65-
GROUP BY Email
66-
HAVING count(Email) > 1;
74+
GROUP BY 1
75+
HAVING count(1) > 1;
6776
```
6877

6978
```sql

solution/0100-0199/0182.Duplicate Emails/README_EN.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,24 @@ Person table:
4949

5050
## Solutions
5151

52+
**Solution 1: Group By + Having**
53+
54+
We can use the `GROUP BY` statement to group the data by the `email` field, and then use the `HAVING` statement to filter out the `email` addresses that appear more than once.
55+
56+
**Solution 2: Self-Join**
57+
58+
We can use a self-join to join the `Person` table with itself, and then filter out the records where the `id` is different but the `email` is the same.
59+
5260
<!-- tabs:start -->
5361

5462
### **SQL**
5563

5664
```sql
57-
SELECT Email
65+
# Write your MySQL query statement below
66+
SELECT email
5867
FROM Person
59-
GROUP BY Email
60-
HAVING count(Email) > 1;
68+
GROUP BY 1
69+
HAVING count(1) > 1;
6170
```
6271

6372
```sql
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
SELECT Email
1+
# Write your MySQL query statement below
2+
SELECT email
23
FROM Person
3-
GROUP BY Email
4-
HAVING count(Email) > 1;
4+
GROUP BY 1
5+
HAVING count(1) > 1;

solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ ActorDirector 表:
5454

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

57-
**方法一:使用 `GROUP BY` + `HAVING`**
57+
**方法一:分组统计**
5858

59-
我们将 `ActorDirector` 表按照 `actor_id``director_id` 进行分组,然后使用 `HAVING` 过滤出合作次数大于等于 $3$ 次的组
59+
我们可以使用 `GROUP BY` 语句,按照 `actor_id``director_id` 字段进行分组,然后使用 `HAVING` 语句,筛选出现次数大于等于 $3$ `actor_id``director_id`
6060

6161
<!-- tabs:start -->
6262

solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ ActorDirector table:
5353

5454
## Solutions
5555

56-
Use `GROUP BY` & `HAVING`.
56+
**Solution 1: Group By + Having**
57+
58+
We can use the `GROUP BY` statement to group the data by the `actor_id` and `director_id` fields, and then use the `HAVING` statement to filter out the `actor_id` and `director_id` that appear at least three times.
5759

5860
<!-- tabs:start -->
5961

solution/1400-1499/1495.Friendly Movies Streamed Last Month/README.md

+10-15
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,24 @@ TVProgram</code> 表:
9191

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

94+
**方法一:等值连接 + 条件筛选**
95+
96+
我们可以先通过等值连接将两张表按照 `content_id` 字段连接起来,然后再通过条件筛选出在 $2020$ 年 $6$ 月份播放的儿童适宜电影。
97+
9498
<!-- tabs:start -->
9599

96100
### **SQL**
97101

98102
```sql
99-
SELECT DISTINCT
100-
title
103+
# Write your MySQL query statement below
104+
SELECT DISTINCT title
101105
FROM
102-
Content
103-
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
106+
TVProgram
107+
JOIN Content USING (content_id)
104108
WHERE
105-
content_type = 'Movies'
109+
date_format(program_date, '%Y%m') = '202006'
106110
AND kids_content = 'Y'
107-
AND program_date BETWEEN '2020-06-01' AND '2020-06-30';
108-
```
109-
110-
```sql
111-
SELECT DISTINCT
112-
title
113-
FROM
114-
Content
115-
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
116-
WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020);
111+
AND content_type = 'Movies';
117112
```
118113

119114
<!-- tabs:end -->

solution/1400-1499/1495.Friendly Movies Streamed Last Month/README_EN.md

+10-15
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,22 @@ Content table:
8787

8888
## Solutions
8989

90+
**Solution 1: Equi-Join + Conditional Filtering**
91+
92+
We can first use an equi-join to join the two tables based on the `content_id` field, and then use conditional filtering to select the child-friendly movies that were played in June 2020.
93+
9094
<!-- tabs:start -->
9195

9296
```sql
93-
SELECT DISTINCT
94-
title
97+
# Write your MySQL query statement below
98+
SELECT DISTINCT title
9599
FROM
96-
Content
97-
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
100+
TVProgram
101+
JOIN Content USING (content_id)
98102
WHERE
99-
content_type = 'Movies'
103+
date_format(program_date, '%Y%m') = '202006'
100104
AND kids_content = 'Y'
101-
AND program_date BETWEEN '2020-06-01' AND '2020-06-30';
102-
```
103-
104-
```sql
105-
SELECT DISTINCT
106-
title
107-
FROM
108-
Content
109-
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
110-
WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020);
105+
AND content_type = 'Movies';
111106
```
112107

113108
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
SELECT DISTINCT
2-
title
1+
# Write your MySQL query statement below
2+
SELECT DISTINCT title
33
FROM
4-
Content
5-
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
4+
TVProgram
5+
JOIN Content USING (content_id)
66
WHERE
7-
content_type = 'Movies'
7+
date_format(program_date, '%Y%m') = '202006'
88
AND kids_content = 'Y'
9-
AND program_date BETWEEN '2020-06-01' AND '2020-06-30';
10-
11-
SELECT DISTINCT
12-
title
13-
FROM
14-
Content
15-
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
16-
WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020);
9+
AND content_type = 'Movies';

solution/1500-1599/1511.Customer Order Frequency/README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,26 @@ Moustafa 在2020年6月花费了110 (10 * 2 + 45 * 2), 在7月花费了0.</pre>
117117

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

120+
**方法一:等值连接 + 分组求和**
121+
122+
我们可以使用 `JOIN` 语句,连接 `Orders``Product` 表,再连接 `Customers` 表,筛选出 `order_date` 在 $2020$ 年的记录,然后使用 `GROUP BY` 语句,按照 `customer_id` 分组,使用 `HAVING` 语句,筛选出 $6$ 月和 $7$ 月花费大于等于 $100$ 的客户。
123+
120124
<!-- tabs:start -->
121125

122126
### **SQL**
123127

124128
```sql
125129
# Write your MySQL query statement below
126-
SELECT
127-
c.customer_id AS CUSTOMER_ID,
128-
c.name AS NAME
130+
SELECT customer_id, name
129131
FROM
130-
Customers c, Product p, Orders o
131-
WHERE
132-
c.customer_id = o.customer_id
133-
AND p.product_id = o.product_id
134-
GROUP BY c.customer_id
135-
HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100
136-
AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100;
132+
Orders
133+
JOIN Product USING (product_id)
134+
JOIN Customers USING (customer_id)
135+
WHERE year(order_date) = 2020
136+
GROUP BY 1
137+
HAVING
138+
sum(if(month(order_date) = 6, quantity * price, 0)) >= 100
139+
AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100;
137140
```
138141

139142
<!-- tabs:end -->

solution/1500-1599/1511.Customer Order Frequency/README_EN.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,26 @@ Moustafa spent 110 (10 * 2 + 45 * 2) in June and 0 in July 2020.
112112

113113
## Solutions
114114

115+
**Solution 1: Join + Group By + Having**
116+
117+
We can use the `JOIN` statement to join the `Orders` table and the `Product` table, and then join the result with the `Customers` table. We can filter out the records where the `order_date` is not in the year $2020$, and then use the `GROUP BY` statement to group the data by `customer_id`. Finally, we can use the `HAVING` statement to filter out the customers whose spending in June and July is greater than or equal to $100$.
118+
115119
<!-- tabs:start -->
116120

117121
### **SQL**
118122

119123
```sql
120124
# Write your MySQL query statement below
121-
SELECT
122-
c.customer_id AS CUSTOMER_ID,
123-
c.name AS NAME
125+
SELECT customer_id, name
124126
FROM
125-
Customers c, Product p, Orders o
126-
WHERE
127-
c.customer_id = o.customer_id
128-
AND p.product_id = o.product_id
129-
GROUP BY c.customer_id
130-
HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100
131-
AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100;
127+
Orders
128+
JOIN Product USING (product_id)
129+
JOIN Customers USING (customer_id)
130+
WHERE year(order_date) = 2020
131+
GROUP BY 1
132+
HAVING
133+
sum(if(month(order_date) = 6, quantity * price, 0)) >= 100
134+
AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100;
132135
```
133136

134137
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
c.customer_id AS CUSTOMER_ID,
4-
c.name AS NAME
2+
SELECT customer_id, name
53
FROM
6-
Customers c, Product p, Orders o
7-
WHERE
8-
c.customer_id = o.customer_id
9-
AND p.product_id = o.product_id
10-
GROUP BY c.customer_id
11-
HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100
12-
AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100;
4+
Orders
5+
JOIN Product USING (product_id)
6+
JOIN Customers USING (customer_id)
7+
WHERE year(order_date) = 2020
8+
GROUP BY 1
9+
HAVING
10+
sum(if(month(order_date) = 6, quantity * price, 0)) >= 100
11+
AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100;

solution/1500-1599/1587.Bank Account Summary II/README.md

+8-15
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,26 @@ Charlie 的余额为(6000 + 6000 - 4000) = 8000.
9090

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

93+
**方法一:等值连接 + 分组求和**
94+
95+
我们可以使用等值连接,将 `Users``Transactions` 表按照 `account` 列连接起来,然后按照 `account` 列分组求和,最后筛选出余额大于 $10000$ 的用户。
96+
9397
<!-- tabs:start -->
9498

9599
### **SQL**
96100

97101
<!-- 这里可写当前语言的特殊实现逻辑 -->
98102

99-
```sql
100-
SELECT
101-
u.name,
102-
SUM(t.amount) AS balance
103-
FROM
104-
users AS u
105-
JOIN transactions AS t ON u.account = t.account
106-
GROUP BY name
107-
HAVING SUM(t.amount) > 10000;
108-
```
109-
110103
```sql
111104
# Write your MySQL query statement below
112105
SELECT
113106
name,
114107
sum(amount) AS balance
115108
FROM
116-
Users AS u
117-
LEFT JOIN Transactions AS t ON u.account = t.account
118-
GROUP BY u.account
119-
HAVING sum(amount) > 10000;
109+
Users
110+
JOIN Transactions USING (account)
111+
GROUP BY account
112+
HAVING balance > 10000;
120113
```
121114

122115
<!-- tabs:end -->

solution/1500-1599/1587.Bank Account Summary II/README_EN.md

+8-15
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,24 @@ Charlie&#39;s balance is (6000 + 6000 - 4000) = 8000.
8484

8585
## Solutions
8686

87+
**Solution 1: Equi-Join + Group By + Sum**
88+
89+
We can use an equi-join to join the `Users` table and the `Transactions` table on the condition of `account`, and then group by `account` to calculate the balance for each account using the `SUM` function. Finally, we can filter out the users whose balance is less than or equal to $10000$.
90+
8791
<!-- tabs:start -->
8892

8993
### **SQL**
9094

91-
```sql
92-
SELECT
93-
u.name,
94-
SUM(t.amount) AS balance
95-
FROM
96-
users AS u
97-
JOIN transactions AS t ON u.account = t.account
98-
GROUP BY name
99-
HAVING SUM(t.amount) > 10000;
100-
```
101-
10295
```sql
10396
# Write your MySQL query statement below
10497
SELECT
10598
name,
10699
sum(amount) AS balance
107100
FROM
108-
Users AS u
109-
LEFT JOIN Transactions AS t ON u.account = t.account
110-
GROUP BY u.account
111-
HAVING sum(amount) > 10000;
101+
Users
102+
JOIN Transactions USING (account)
103+
GROUP BY account
104+
HAVING balance > 10000;
112105
```
113106

114107
<!-- tabs:end -->

0 commit comments

Comments
 (0)