Skip to content

Commit 0c7a735

Browse files
authored
feat: update sql solutions to lc problems (#1188)
* No.0585.Investments in 2016 * No.0586.Customer Placing the Largest Number of Orders * No.0595.Big Countries * No.0596.Classes More Than 5 Students * No.0597.Friend Requests I Overall Acceptance Rate
1 parent f1f56bc commit 0c7a735

File tree

16 files changed

+99
-82
lines changed

16 files changed

+99
-82
lines changed

solution/0000-0099/0016.3Sum Closest/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
**方法一:排序 + 双指针**
4747

48-
将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。
48+
我们将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。
4949

5050
时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。
5151

solution/0500-0599/0585.Investments in 2016/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,16 @@ tiv_2015 值为 10 与第三条和第四条记录相同,且其位置是唯一
8080
```sql
8181
# Write your MySQL query statement below
8282
WITH
83-
t AS (
83+
T AS (
8484
SELECT
8585
tiv_2016,
8686
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
87-
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
87+
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
8888
FROM Insurance
8989
)
90-
SELECT
91-
round(sum(TIV_2016), 2) AS tiv_2016
92-
FROM t
93-
WHERE cnt1 != 1 AND cnt2 = 1;
90+
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
91+
FROM T
92+
WHERE cnt1 > 1 AND cnt2 = 1;
9493
```
9594

9695
<!-- tabs:end -->

solution/0500-0599/0585.Investments in 2016/README_EN.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ So, the result is the sum of tiv_2016 of the first and last record, which is 45.
7474
```sql
7575
# Write your MySQL query statement below
7676
WITH
77-
t AS (
77+
T AS (
7878
SELECT
7979
tiv_2016,
8080
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
81-
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
81+
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
8282
FROM Insurance
8383
)
84-
SELECT
85-
round(sum(TIV_2016), 2) AS tiv_2016
86-
FROM t
87-
WHERE cnt1 != 1 AND cnt2 = 1;
84+
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
85+
FROM T
86+
WHERE cnt1 > 1 AND cnt2 = 1;
8887
```
8988

9089
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Write your MySQL query statement below
22
WITH
3-
t AS (
3+
T AS (
44
SELECT
55
tiv_2016,
66
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
7-
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
7+
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
88
FROM Insurance
99
)
10-
SELECT
11-
round(sum(TIV_2016), 2) AS tiv_2016
12-
FROM t
13-
WHERE cnt1 != 1 AND cnt2 = 1;
10+
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
11+
FROM T
12+
WHERE cnt1 > 1 AND cnt2 = 1;

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ ORDER BY count(1) DESC
7575
LIMIT 1;
7676
```
7777

78-
SQL Server
79-
8078
```sql
79+
/* Write your T-SQL query statement below */
8180
SELECT TOP 1
8281
customer_number
8382
FROM

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ So the result is customer_number 3.
6161

6262
```sql
6363
# Write your MySQL query statement below
64-
SELECT
65-
customer_number
66-
FROM orders
67-
GROUP BY customer_number
64+
SELECT customer_number
65+
FROM Orders
66+
GROUP BY 1
6867
ORDER BY count(1) DESC
6968
LIMIT 1;
7069
```
7170

72-
SQL Server
73-
7471
```sql
72+
/* Write your T-SQL query statement below */
7573
SELECT TOP 1
7674
customer_number
7775
FROM
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
customer_number
4-
FROM orders
5-
GROUP BY customer_number
2+
SELECT customer_number
3+
FROM Orders
4+
GROUP BY 1
65
ORDER BY count(1) DESC
76
LIMIT 1;

solution/0500-0599/0595.Big Countries/README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,34 @@ World 表:
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75+
**方法一:使用 WHERE + OR**
76+
77+
我们可以使用 `WHERE` + `OR` 查询出所有符合条件的国家。
78+
79+
**方法二:使用 UNION**
80+
81+
我们可以查询出所有面积大于等于 300 万平方公里的国家,然后再查询出所有人口大于等于 2500 万的国家,最后使用 `UNION` 将两个结果集合并起来。
82+
7583
<!-- tabs:start -->
7684

7785
### **SQL**
7886

7987
```sql
80-
SELECT
81-
name,
82-
population,
83-
area
84-
FROM world
85-
WHERE area > 3000000 OR population > 25000000;
88+
# Write your MySQL query statement below
89+
SELECT name, population, area
90+
FROM World
91+
WHERE area >= 3000000 OR population >= 25000000;
92+
```
93+
94+
```sql
95+
# Write your MySQL query statement below
96+
SELECT name, population, area
97+
FROM World
98+
WHERE area >= 3000000
99+
UNION
100+
SELECT name, population, area
101+
FROM World
102+
WHERE population >= 25000000;
86103
```
87104

88105
<!-- tabs:end -->

solution/0500-0599/0595.Big Countries/README_EN.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,21 @@ World table:
6666
### **SQL**
6767

6868
```sql
69-
SELECT
70-
name,
71-
population,
72-
area
73-
FROM world
74-
WHERE area > 3000000 OR population > 25000000;
69+
# Write your MySQL query statement below
70+
SELECT name, population, area
71+
FROM World
72+
WHERE area >= 3000000 OR population >= 25000000;
73+
```
74+
75+
```sql
76+
# Write your MySQL query statement below
77+
SELECT name, population, area
78+
FROM World
79+
WHERE area >= 3000000
80+
UNION
81+
SELECT name, population, area
82+
FROM World
83+
WHERE population >= 25000000;
7584
```
7685

7786
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
SELECT
2-
name,
3-
population,
4-
area
5-
FROM world
6-
WHERE area > 3000000 OR population > 25000000;
1+
# Write your MySQL query statement below
2+
SELECT name, population, area
3+
FROM World
4+
WHERE area >= 3000000 OR population >= 25000000;

solution/0500-0599/0596.Classes More Than 5 Students/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ Courses table:
6868
### **SQL**
6969

7070
```sql
71-
SELECT
72-
class
73-
FROM courses
71+
# Write your MySQL query statement below
72+
SELECT class
73+
FROM Courses
7474
GROUP BY class
75-
HAVING COUNT(class) >= 5;
75+
HAVING count(1) >= 5;
7676
```
7777

7878
<!-- tabs:end -->

solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ Courses table:
6464
### **SQL**
6565

6666
```sql
67-
SELECT
68-
class
69-
FROM courses
67+
# Write your MySQL query statement below
68+
SELECT class
69+
FROM Courses
7070
GROUP BY class
71-
HAVING COUNT(class) >= 5;
71+
HAVING count(1) >= 5;
7272
```
7373

7474
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
SELECT
2-
class
3-
FROM courses
1+
# Write your MySQL query statement below
2+
SELECT class
3+
FROM Courses
44
GROUP BY class
5-
HAVING COUNT(class) >= 5;
5+
HAVING count(1) >= 5;

solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@ RequestAccepted 表:
102102
### **SQL**
103103

104104
```sql
105+
# Write your MySQL query statement below
105106
SELECT
106-
IFNULL(
107-
ROUND(
107+
round(
108+
ifnull(
108109
(
109-
SELECT COUNT(DISTINCT requester_id, accepter_id)
110+
SELECT count(DISTINCT requester_id, accepter_id)
110111
FROM RequestAccepted
111112
) / (
112-
SELECT COUNT(DISTINCT sender_id, send_to_id)
113-
FROM FriendRequest
113+
SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest
114114
),
115-
2
115+
0
116116
),
117-
0.00
117+
2
118118
) AS accept_rate;
119119
```
120120

solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README_EN.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ There are 4 unique accepted requests, and there are 5 requests in total. So the
9898
### **SQL**
9999

100100
```sql
101+
# Write your MySQL query statement below
101102
SELECT
102-
IFNULL(
103-
ROUND(
103+
round(
104+
ifnull(
104105
(
105-
SELECT COUNT(DISTINCT requester_id, accepter_id)
106+
SELECT count(DISTINCT requester_id, accepter_id)
106107
FROM RequestAccepted
107108
) / (
108-
SELECT COUNT(DISTINCT sender_id, send_to_id)
109-
FROM FriendRequest
109+
SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest
110110
),
111-
2
111+
0
112112
),
113-
0.00
113+
2
114114
) AS accept_rate;
115115
```
116116

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
# Write your MySQL query statement below
12
SELECT
2-
IFNULL(
3-
ROUND(
3+
round(
4+
ifnull(
45
(
5-
SELECT COUNT(DISTINCT requester_id, accepter_id)
6+
SELECT count(DISTINCT requester_id, accepter_id)
67
FROM RequestAccepted
78
) / (
8-
SELECT COUNT(DISTINCT sender_id, send_to_id)
9-
FROM FriendRequest
9+
SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest
1010
),
11-
2
11+
0
1212
),
13-
0.00
13+
2
1414
) AS accept_rate;

0 commit comments

Comments
 (0)