Skip to content

Commit 75b76e8

Browse files
authored
feat: add sql solutions to lc problems: No.2984+ (doocs#2172)
1 parent 71f8fdc commit 75b76e8

File tree

24 files changed

+380
-16
lines changed

24 files changed

+380
-16
lines changed

solution/2900-2999/2984.Find Peak Calling Hours for Each City/README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,29 @@ Output table is ordered by peak_calling_hour and city in descending order.</pre>
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```sql
72-
72+
# Write your MySQL query statement below
73+
WITH
74+
T AS (
75+
SELECT
76+
*,
77+
RANK() OVER (
78+
PARTITION BY city
79+
ORDER BY cnt DESC
80+
) AS rk
81+
FROM
82+
(
83+
SELECT
84+
city,
85+
HOUR(call_time) AS h,
86+
COUNT(1) AS cnt
87+
FROM Calls
88+
GROUP BY 1, 2
89+
) AS t
90+
)
91+
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
92+
FROM T
93+
WHERE rk = 1
94+
ORDER BY 2 DESC, 1 DESC;
7395
```
7496

7597
<!-- tabs:end -->

solution/2900-2999/2984.Find Peak Calling Hours for Each City/README_EN.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,29 @@ Output table is ordered by peak_calling_hour and city in descending order.</pre>
6363
### **SQL**
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
WITH
68+
T AS (
69+
SELECT
70+
*,
71+
RANK() OVER (
72+
PARTITION BY city
73+
ORDER BY cnt DESC
74+
) AS rk
75+
FROM
76+
(
77+
SELECT
78+
city,
79+
HOUR(call_time) AS h,
80+
COUNT(1) AS cnt
81+
FROM Calls
82+
GROUP BY 1, 2
83+
) AS t
84+
)
85+
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
86+
FROM T
87+
WHERE rk = 1
88+
ORDER BY 2 DESC, 1 DESC;
6789
```
6890

6991
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
RANK() OVER (
7+
PARTITION BY city
8+
ORDER BY cnt DESC
9+
) AS rk
10+
FROM
11+
(
12+
SELECT
13+
city,
14+
HOUR(call_time) AS h,
15+
COUNT(1) AS cnt
16+
FROM Calls
17+
GROUP BY 1, 2
18+
) AS t
19+
)
20+
SELECT city, h AS peak_calling_hour, cnt AS number_of_calls
21+
FROM T
22+
WHERE rk = 1
23+
ORDER BY 2 DESC, 1 DESC;

solution/2900-2999/2985.Calculate Compressed Mean/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ The calculation is as follows:
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
SELECT
68+
ROUND(
69+
SUM(item_count * order_occurrences) / SUM(order_occurrences),
70+
2
71+
) AS average_items_per_order
72+
FROM Orders;
6773
```
6874

6975
<!-- tabs:end -->

solution/2900-2999/2985.Calculate Compressed Mean/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ The calculation is as follows:
5757
### **SQL**
5858

5959
```sql
60-
60+
# Write your MySQL query statement below
61+
SELECT
62+
ROUND(
63+
SUM(item_count * order_occurrences) / SUM(order_occurrences),
64+
2
65+
) AS average_items_per_order
66+
FROM Orders;
6167
```
6268

6369
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
ROUND(
4+
SUM(item_count * order_occurrences) / SUM(order_occurrences),
5+
2
6+
) AS average_items_per_order
7+
FROM Orders;

solution/2900-2999/2986.Find Third Transaction/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,32 @@ Output table is ordered by user_id in ascending order.
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```sql
73-
73+
# Write your MySQL query statement below
74+
WITH
75+
T AS (
76+
SELECT
77+
*,
78+
RANK() OVER (
79+
PARTITION BY user_id
80+
ORDER BY transaction_date
81+
) AS rk,
82+
spend > (
83+
LAG(spend) OVER (
84+
PARTITION BY user_id
85+
ORDER BY transaction_date
86+
)
87+
)
88+
AND spend > (
89+
LAG(spend, 2) OVER (
90+
PARTITION BY user_id
91+
ORDER BY transaction_date
92+
)
93+
) AS st
94+
FROM Transactions
95+
)
96+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
97+
FROM T
98+
WHERE rk = 3 AND st = 1;
7499
```
75100

76101
<!-- tabs:end -->

solution/2900-2999/2986.Find Third Transaction/README_EN.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,32 @@ Output table is ordered by user_id in ascending order.
6464
### **SQL**
6565

6666
```sql
67-
67+
# Write your MySQL query statement below
68+
WITH
69+
T AS (
70+
SELECT
71+
*,
72+
RANK() OVER (
73+
PARTITION BY user_id
74+
ORDER BY transaction_date
75+
) AS rk,
76+
spend > (
77+
LAG(spend) OVER (
78+
PARTITION BY user_id
79+
ORDER BY transaction_date
80+
)
81+
)
82+
AND spend > (
83+
LAG(spend, 2) OVER (
84+
PARTITION BY user_id
85+
ORDER BY transaction_date
86+
)
87+
) AS st
88+
FROM Transactions
89+
)
90+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
91+
FROM T
92+
WHERE rk = 3 AND st = 1;
6893
```
6994

7095
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
RANK() OVER (
7+
PARTITION BY user_id
8+
ORDER BY transaction_date
9+
) AS rk,
10+
spend > (
11+
LAG(spend) OVER (
12+
PARTITION BY user_id
13+
ORDER BY transaction_date
14+
)
15+
)
16+
AND spend > (
17+
LAG(spend, 2) OVER (
18+
PARTITION BY user_id
19+
ORDER BY transaction_date
20+
)
21+
) AS st
22+
FROM Transactions
23+
)
24+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
25+
FROM T
26+
WHERE rk = 3 AND st = 1;

solution/2900-2999/2987.Find Expensive Cities/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave
7575
<!-- 这里可写当前语言的特殊实现逻辑 -->
7676

7777
```sql
78-
78+
# Write your MySQL query statement below
79+
SELECT city
80+
FROM Listings
81+
GROUP BY city
82+
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
83+
ORDER BY 1;
7984
```
8085

8186
<!-- tabs:end -->

solution/2900-2999/2987.Find Expensive Cities/README_EN.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave
6969
### **SQL**
7070

7171
```sql
72-
72+
# Write your MySQL query statement below
73+
SELECT city
74+
FROM Listings
75+
GROUP BY city
76+
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
77+
ORDER BY 1;
7378
```
7479

7580
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
SELECT city
3+
FROM Listings
4+
GROUP BY city
5+
HAVING AVG(price) > (SELECT AVG(price) FROM Listings)
6+
ORDER BY 1;

solution/2900-2999/2988.Manager of the Largest Department/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,19 @@ Output table is ordered by dep_id in ascending order.
7272
<!-- 这里可写当前语言的特殊实现逻辑 -->
7373

7474
```sql
75-
75+
# Write your MySQL query statement below
76+
WITH
77+
T AS (
78+
SELECT dep_id, COUNT(1) AS cnt
79+
FROM Employees
80+
GROUP BY 1
81+
)
82+
SELECT emp_name AS manager_name, t.dep_id
83+
FROM
84+
T AS t
85+
JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager'
86+
WHERE cnt = (SELECT MAX(cnt) FROM T)
87+
ORDER BY 2;
7688
```
7789

7890
<!-- tabs:end -->

solution/2900-2999/2988.Manager of the Largest Department/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ Output table is ordered by dep_id in ascending order.
6666
### **SQL**
6767

6868
```sql
69-
69+
# Write your MySQL query statement below
70+
WITH
71+
T AS (
72+
SELECT dep_id, COUNT(1) AS cnt
73+
FROM Employees
74+
GROUP BY 1
75+
)
76+
SELECT emp_name AS manager_name, t.dep_id
77+
FROM
78+
T AS t
79+
JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager'
80+
WHERE cnt = (SELECT MAX(cnt) FROM T)
81+
ORDER BY 2;
7082
```
7183

7284
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT dep_id, COUNT(1) AS cnt
5+
FROM Employees
6+
GROUP BY 1
7+
)
8+
SELECT emp_name AS manager_name, t.dep_id
9+
FROM
10+
T AS t
11+
JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager'
12+
WHERE cnt = (SELECT MAX(cnt) FROM T)
13+
ORDER BY 2;

solution/2900-2999/2989.Class Performance/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,23 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest
6464

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

67+
**方法一:最大值最小值**
68+
69+
我们可以使用 `MAX``MIN` 函数来分别获取 `assignment1``assignment2``assignment3` 的和的最大值和最小值,然后相减即可。
70+
6771
<!-- tabs:start -->
6872

6973
### **SQL**
7074

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

7377
```sql
74-
78+
# Write your MySQL query statement below
79+
SELECT
80+
MAX(assignment1 + assignment2 + assignment3) - MIN(
81+
assignment1 + assignment2 + assignment3
82+
) AS difference_in_score
83+
FROM Scores;
7584
```
7685

7786
<!-- tabs:end -->

solution/2900-2999/2989.Class Performance/README_EN.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,21 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest
6060

6161
## Solutions
6262

63+
**Solution 1: Maximum and Minimum**
64+
65+
We can use the `MAX` and `MIN` functions to get the maximum and minimum sums of `assignment1`, `assignment2`, and `assignment3`, respectively. Then, subtract the minimum from the maximum.
66+
6367
<!-- tabs:start -->
6468

6569
### **SQL**
6670

6771
```sql
68-
72+
# Write your MySQL query statement below
73+
SELECT
74+
MAX(assignment1 + assignment2 + assignment3) - MIN(
75+
assignment1 + assignment2 + assignment3
76+
) AS difference_in_score
77+
FROM Scores;
6978
```
7079

7180
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
MAX(assignment1 + assignment2 + assignment3) - MIN(
4+
assignment1 + assignment2 + assignment3
5+
) AS difference_in_score
6+
FROM Scores;

solution/2900-2999/2990.Loan Types/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,23 @@ Output table is ordered by user_id in ascending order.
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:分组求和**
66+
67+
我们可以对 `Loans` 表按照 `user_id` 进行分组,找出既包含 `Refinance` 又包含 `Mortgage` 的用户,然后按照 `user_id` 进行排序。
68+
6569
<!-- tabs:start -->
6670

6771
### **SQL**
6872

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

7175
```sql
72-
76+
# Write your MySQL query statement below
77+
SELECT user_id
78+
FROM Loans
79+
GROUP BY 1
80+
HAVING SUM(loan_type = 'Refinance') > 0 AND SUM(loan_type = 'Mortgage') > 0
81+
ORDER BY 1;
7382
```
7483

7584
<!-- tabs:end -->

0 commit comments

Comments
 (0)