Skip to content

Commit 81e5b16

Browse files
authored
feat: add sql solutions to lc problems (#1121)
* No.1990.Count the Number of Experiments * No.2020.Number of Accounts That Did Not Stream * No.2051.The Category of Each Member in the Store * No.2066.Account Balance * No.2084.Drop Type 1 Orders for Customers With Type 0 Orders * No.2112.The Airport With the Most Traffic
1 parent 18dbf9d commit 81e5b16

File tree

25 files changed

+301
-33
lines changed

25 files changed

+301
-33
lines changed

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"solution/1600-1699/1667.Fix Names in a Table/Solution.sql",
3434
"solution/1700-1799/1777.Product's Price for Each Store/Solution.sql",
3535
"solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql",
36+
"solution/2000-2099/2066.Account Balance/Solution.sql",
3637
"solution/2600-2699/2686.Immediate Food Delivery III/Solution.sql",
3738
"solution/2700-2799/2752.Customers with Maximum Number of Transactions on Consecutive Days/Solution.sql"
3839
],

solution/0100-0199/0192.Word Frequency/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ day 1
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```sh
53-
53+
# Read from the file words.txt and output the word frequency list to stdout.
54+
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
5455
```
5556

5657
<!-- tabs:end -->

solution/0100-0199/0192.Word Frequency/README_EN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ day 1
4646
### **Bash**
4747

4848
```sh
49-
49+
# Read from the file words.txt and output the word frequency list to stdout.
50+
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
5051
```
5152

5253
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,2 @@
1-
#!/usr/bin/env bash
2-
3-
# NF是当前行的field字段数;NR是正在处理的当前行数。
4-
awk '{
5-
for(i=1;i<=NF;i++){
6-
res[$i]++
7-
}
8-
}
9-
END{
10-
for(k in res){
11-
print k,res[k]
12-
}
13-
}' words.txt | sort -nr -k2
14-
15-
#or:
16-
17-
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2" "$1}'
1+
# Read from the file words.txt and output the word frequency list to stdout.
2+
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'

solution/1900-1999/1990.Count the Number of Experiments/README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,33 @@ Experiments table:
7979
<!-- 这里可写当前语言的特殊实现逻辑 -->
8080

8181
```sql
82-
82+
# Write your MySQL query statement below
83+
WITH
84+
P AS (
85+
SELECT 'Android' AS platform
86+
UNION
87+
SELECT 'IOS'
88+
UNION
89+
SELECT 'Web'
90+
),
91+
Exp AS (
92+
SELECT 'Reading' AS experiment_name
93+
UNION
94+
SELECT 'Sports'
95+
UNION
96+
SELECT 'Programming'
97+
),
98+
T AS (
99+
SELECT *
100+
FROM
101+
P,
102+
Exp
103+
)
104+
SELECT platform, experiment_name, count(experiment_id) AS num_experiments
105+
FROM
106+
T AS t
107+
LEFT JOIN Experiments USING (platform, experiment_name)
108+
GROUP BY 1, 2;
83109
```
84110

85111
<!-- tabs:end -->

solution/1900-1999/1990.Count the Number of Experiments/README_EN.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,33 @@ On the platform &quot;Web&quot;, we had two &quot;Reading&quot; experiments and
7171
### **SQL**
7272

7373
```sql
74-
74+
# Write your MySQL query statement below
75+
WITH
76+
P AS (
77+
SELECT 'Android' AS platform
78+
UNION
79+
SELECT 'IOS'
80+
UNION
81+
SELECT 'Web'
82+
),
83+
Exp AS (
84+
SELECT 'Reading' AS experiment_name
85+
UNION
86+
SELECT 'Sports'
87+
UNION
88+
SELECT 'Programming'
89+
),
90+
T AS (
91+
SELECT *
92+
FROM
93+
P,
94+
Exp
95+
)
96+
SELECT platform, experiment_name, count(experiment_id) AS num_experiments
97+
FROM
98+
T AS t
99+
LEFT JOIN Experiments USING (platform, experiment_name)
100+
GROUP BY 1, 2;
75101
```
76102

77103
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
P AS (
4+
SELECT 'Android' AS platform
5+
UNION
6+
SELECT 'IOS'
7+
UNION
8+
SELECT 'Web'
9+
),
10+
Exp AS (
11+
SELECT 'Reading' AS experiment_name
12+
UNION
13+
SELECT 'Sports'
14+
UNION
15+
SELECT 'Programming'
16+
),
17+
T AS (
18+
SELECT *
19+
FROM
20+
P,
21+
Exp
22+
)
23+
SELECT platform, experiment_name, count(experiment_id) AS num_experiments
24+
FROM
25+
T AS t
26+
LEFT JOIN Experiments USING (platform, experiment_name)
27+
GROUP BY 1, 2;

solution/2000-2099/2020.Number of Accounts That Did Not Stream/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ Streams table:
8989
<!-- 这里可写当前语言的特殊实现逻辑 -->
9090

9191
```sql
92-
92+
# Write your MySQL query statement below
93+
SELECT count(DISTINCT sub.account_id) AS accounts_count
94+
FROM
95+
Subscriptions AS sub
96+
LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id
97+
WHERE
98+
year(start_date) <= 2021
99+
AND year(end_date) >= 2021
100+
AND (year(stream_date) != 2021 OR stream_date > end_date);
93101
```
94102

95103
<!-- tabs:end -->

solution/2000-2099/2020.Number of Accounts That Did Not Stream/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ User 11 did not subscribe in 2021.
8686
### **SQL**
8787

8888
```sql
89-
89+
# Write your MySQL query statement below
90+
SELECT count(DISTINCT sub.account_id) AS accounts_count
91+
FROM
92+
Subscriptions AS sub
93+
LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id
94+
WHERE
95+
year(start_date) <= 2021
96+
AND year(end_date) >= 2021
97+
AND (year(stream_date) != 2021 OR stream_date > end_date);
9098
```
9199

92100
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write your MySQL query statement below
2+
SELECT count(DISTINCT sub.account_id) AS accounts_count
3+
FROM
4+
Subscriptions AS sub
5+
LEFT JOIN Streams AS ss ON sub.account_id = ss.account_id
6+
WHERE
7+
year(start_date) <= 2021
8+
AND year(end_date) >= 2021
9+
AND (year(stream_date) != 2021 OR stream_date > end_date);

solution/2000-2099/2041.Accepted Candidates From the Interviews/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ Rounds table:
101101
<!-- 这里可写当前语言的特殊实现逻辑 -->
102102

103103
```sql
104-
104+
# Write your MySQL query statement below
105+
SELECT candidate_id
106+
FROM
107+
Candidates AS c
108+
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
109+
WHERE years_of_exp >= 2
110+
GROUP BY c.interview_id
111+
HAVING sum(score) > 15;
105112
```
106113

107114
<!-- tabs:end -->

solution/2000-2099/2041.Accepted Candidates From the Interviews/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ Rounds table:
9494
### **SQL**
9595

9696
```sql
97-
97+
# Write your MySQL query statement below
98+
SELECT candidate_id
99+
FROM
100+
Candidates AS c
101+
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
102+
WHERE years_of_exp >= 2
103+
GROUP BY c.interview_id
104+
HAVING sum(score) > 15;
98105
```
99106

100107
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT candidate_id
3+
FROM
4+
Candidates AS c
5+
LEFT JOIN Rounds AS r ON c.interview_id = r.interview_id
6+
WHERE years_of_exp >= 2
7+
GROUP BY c.interview_id
8+
HAVING sum(score) > 15;

solution/2000-2099/2051.The Category of Each Member in the Store/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,23 @@ Purchases 表:
135135
<!-- 这里可写当前语言的特殊实现逻辑 -->
136136

137137
```sql
138-
138+
# Write your MySQL query statement below
139+
SELECT
140+
m.member_id,
141+
name,
142+
CASE
143+
WHEN count(v.visit_id) = 0 THEN 'Bronze'
144+
WHEN 100 * count(charged_amount) / count(
145+
v.visit_id
146+
) >= 80 THEN 'Diamond'
147+
WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold'
148+
ELSE 'Silver'
149+
END AS category
150+
FROM
151+
Members AS m
152+
LEFT JOIN Visits AS v ON m.member_id = v.member_id
153+
LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id
154+
GROUP BY member_id;
139155
```
140156

141157
<!-- tabs:end -->

solution/2000-2099/2051.The Category of Each Member in the Store/README_EN.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,23 @@ Purchases table:
129129
### **SQL**
130130

131131
```sql
132-
132+
# Write your MySQL query statement below
133+
SELECT
134+
m.member_id,
135+
name,
136+
CASE
137+
WHEN count(v.visit_id) = 0 THEN 'Bronze'
138+
WHEN 100 * count(charged_amount) / count(
139+
v.visit_id
140+
) >= 80 THEN 'Diamond'
141+
WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold'
142+
ELSE 'Silver'
143+
END AS category
144+
FROM
145+
Members AS m
146+
LEFT JOIN Visits AS v ON m.member_id = v.member_id
147+
LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id
148+
GROUP BY member_id;
133149
```
134150

135151
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
m.member_id,
4+
name,
5+
CASE
6+
WHEN count(v.visit_id) = 0 THEN 'Bronze'
7+
WHEN 100 * count(charged_amount) / count(
8+
v.visit_id
9+
) >= 80 THEN 'Diamond'
10+
WHEN 100 * count(charged_amount) / count(v.visit_id) >= 50 THEN 'Gold'
11+
ELSE 'Silver'
12+
END AS category
13+
FROM
14+
Members AS m
15+
LEFT JOIN Visits AS v ON m.member_id = v.member_id
16+
LEFT JOIN Purchases AS p ON v.visit_id = p.visit_id
17+
GROUP BY member_id;

solution/2000-2099/2066.Account Balance/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ Transactions 表:
8080
<!-- 这里可写当前语言的特殊实现逻辑 -->
8181

8282
```sql
83-
83+
# Write your MySQL query statement below
84+
SELECT
85+
account_id,
86+
day,
87+
sum(if(type = 'Deposit', amount, -amount)) OVER (
88+
PARTITION BY account_id
89+
ORDER BY day
90+
) AS balance
91+
FROM Transactions
92+
ORDER BY 1, 2;
8493
```
8594

8695
<!-- tabs:end -->

solution/2000-2099/2066.Account Balance/README_EN.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ Account 2:
7272
### **SQL**
7373

7474
```sql
75-
75+
# Write your MySQL query statement below
76+
SELECT
77+
account_id,
78+
day,
79+
sum(if(type = 'Deposit', amount, -amount)) OVER (
80+
PARTITION BY account_id
81+
ORDER BY day
82+
) AS balance
83+
FROM Transactions
84+
ORDER BY 1, 2;
7685
```
7786

7887
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
account_id,
4+
day,
5+
sum(if(type = 'Deposit', amount, -amount)) OVER (
6+
PARTITION BY account_id
7+
ORDER BY day
8+
) AS balance
9+
FROM Transactions
10+
ORDER BY 1, 2;

solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@ Orders table:
8080
<!-- 这里可写当前语言的特殊实现逻辑 -->
8181

8282
```sql
83-
83+
# Write your MySQL query statement below
84+
WITH
85+
T AS (
86+
SELECT DISTINCT customer_id
87+
FROM Orders
88+
WHERE order_type = 0
89+
)
90+
SELECT *
91+
FROM Orders AS o
92+
WHERE
93+
order_type = 0
94+
OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id);
8495
```
8596

8697
<!-- tabs:end -->

solution/2000-2099/2084.Drop Type 1 Orders for Customers With Type 0 Orders/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,18 @@ Customer 4 has two orders of type 1. We return both of them.
7676
### **SQL**
7777

7878
```sql
79-
79+
# Write your MySQL query statement below
80+
WITH
81+
T AS (
82+
SELECT DISTINCT customer_id
83+
FROM Orders
84+
WHERE order_type = 0
85+
)
86+
SELECT *
87+
FROM Orders AS o
88+
WHERE
89+
order_type = 0
90+
OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id);
8091
```
8192

8293
<!-- tabs:end -->

0 commit comments

Comments
 (0)