Skip to content

Commit 4769d32

Browse files
authored
feat: update sql solutions to lc problems: No.0607,1112,1407,1607 (#1781)
* No.0607.Sales Person * No.1112.Highest Grade For Each Student * No.1407.Top Travellers * No.1607.Sellers With No Sales
1 parent 52193fa commit 4769d32

File tree

11 files changed

+112
-109
lines changed

11 files changed

+112
-109
lines changed

solution/0100-0199/0175.Combine Two Tables/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ addressId = 1 contains information about the address of personId = 2.
7676

7777
## Solutions
7878

79+
**Solution 1: LEFT JOIN**
80+
81+
We can use a left join to join the `Person` table with the `Address` table on the condition `Person.personId = Address.personId`, which will give us the first name, last name, city, and state of each person. If the address of a `personId` is not in the `Address` table, it will be reported as `null`.
82+
7983
<!-- tabs:start -->
8084

8185
### **SQL**

solution/0600-0699/0607.Sales Person/README.md

+11-40
Original file line numberDiff line numberDiff line change
@@ -116,52 +116,23 @@ Orders 表:
116116

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

119-
<!-- tabs:start -->
119+
**方法一:左连接 + 分组统计**
120120

121-
### **SQL**
121+
我们可以使用左连接将 `SalesPerson` 表与 `Orders` 表连接起来,再与 `Company` 表连接起来,然后按照 `sales_id` 分组,每组统计有多少个公司的名字为 `RED` 的订单,最后筛选出没有这样的订单的销售人员的姓名。
122122

123-
```sql
124-
SELECT name
125-
FROM salesperson
126-
WHERE
127-
sales_id NOT IN (
128-
SELECT s.sales_id
129-
FROM
130-
orders AS o
131-
INNER JOIN salesperson AS s ON o.sales_id = s.sales_id
132-
INNER JOIN company AS c ON o.com_id = c.com_id
133-
WHERE c.name = 'RED'
134-
);
135-
```
123+
<!-- tabs:start -->
136124

137-
```sql
138-
SELECT
139-
name
140-
FROM SalesPerson AS s
141-
WHERE
142-
0 = (
143-
SELECT
144-
COUNT(*)
145-
FROM
146-
Orders AS o
147-
JOIN Company AS c ON o.com_id = c.com_id
148-
WHERE o.sales_id = s.sales_id AND c.name = 'RED'
149-
);
150-
```
125+
### **SQL**
151126

152127
```sql
153128
# Write your MySQL query statement below
154-
SELECT name
155-
FROM SalesPerson
156-
WHERE
157-
sales_id NOT IN (
158-
SELECT sales_id
159-
FROM
160-
Company AS c
161-
JOIN Orders AS o USING (com_id)
162-
GROUP BY sales_id
163-
HAVING sum(name = 'RED') > 0
164-
);
129+
SELECT s.name
130+
FROM
131+
SalesPerson AS s
132+
LEFT JOIN Orders USING (sales_id)
133+
LEFT JOIN Company AS c USING (com_id)
134+
GROUP BY sales_id
135+
HAVING ifnull(sum(c.name = 'RED'), 0) = 0;
165136
```
166137

167138
<!-- tabs:end -->

solution/0600-0699/0607.Sales Person/README_EN.md

+11-40
Original file line numberDiff line numberDiff line change
@@ -111,52 +111,23 @@ According to orders 3 and 4 in the Orders table, it is easy to tell that only sa
111111

112112
## Solutions
113113

114-
<!-- tabs:start -->
114+
**Solution 1: LEFT JOIN + GROUP BY**
115115

116-
### **SQL**
116+
We can use a left join to join the `SalesPerson` table with the `Orders` table on the condition of sales id, and then join the result with the `Company` table on the condition of company id. After that, we can group by `sales_id` and count the number of orders with the company name `RED`. Finally, we can filter out the salespersons who do not have any orders with the company name `RED`.
117117

118-
```sql
119-
SELECT name
120-
FROM salesperson
121-
WHERE
122-
sales_id NOT IN (
123-
SELECT s.sales_id
124-
FROM
125-
orders AS o
126-
INNER JOIN salesperson AS s ON o.sales_id = s.sales_id
127-
INNER JOIN company AS c ON o.com_id = c.com_id
128-
WHERE c.name = 'RED'
129-
);
130-
```
118+
<!-- tabs:start -->
131119

132-
```sql
133-
SELECT
134-
name
135-
FROM SalesPerson AS s
136-
WHERE
137-
0 = (
138-
SELECT
139-
COUNT(*)
140-
FROM
141-
Orders AS o
142-
JOIN Company AS c ON o.com_id = c.com_id
143-
WHERE o.sales_id = s.sales_id AND c.name = 'RED'
144-
);
145-
```
120+
### **SQL**
146121

147122
```sql
148123
# Write your MySQL query statement below
149-
SELECT name
150-
FROM SalesPerson
151-
WHERE
152-
sales_id NOT IN (
153-
SELECT sales_id
154-
FROM
155-
Company AS c
156-
JOIN Orders AS o USING (com_id)
157-
GROUP BY sales_id
158-
HAVING sum(name = 'RED') > 0
159-
);
124+
SELECT s.name
125+
FROM
126+
SalesPerson AS s
127+
LEFT JOIN Orders USING (sales_id)
128+
LEFT JOIN Company AS c USING (com_id)
129+
GROUP BY sales_id
130+
HAVING ifnull(sum(c.name = 'RED'), 0) = 0;
160131
```
161132

162133
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
SELECT name
2-
FROM salesperson
3-
WHERE
4-
sales_id NOT IN (
5-
SELECT s.sales_id
6-
FROM
7-
orders AS o
8-
INNER JOIN salesperson AS s ON o.sales_id = s.sales_id
9-
INNER JOIN company AS c ON o.com_id = c.com_id
10-
WHERE c.name = 'RED'
11-
);
1+
# Write your MySQL query statement below
2+
SELECT s.name
3+
FROM
4+
SalesPerson AS s
5+
LEFT JOIN Orders USING (sales_id)
6+
LEFT JOIN Company AS c USING (com_id)
7+
GROUP BY sales_id
8+
HAVING ifnull(sum(c.name = 'RED'), 0) = 0;

solution/1100-1199/1112.Highest Grade For Each Student/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ Enrollments 表:
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:RANK() OVER() 窗口函数**
62+
63+
我们可以使用 `RANK() OVER()` 窗口函数,按照每个学生的成绩降序排列,如果成绩相同,按照课程号升序排列,然后取每个学生排名为 $1$ 的记录。
64+
65+
**方法二:子查询**
66+
67+
我们可以先查询每个学生的最高成绩,然后再查询每个学生的最高成绩对应的最小课程号。
68+
6169
<!-- tabs:start -->
6270

6371
### **SQL**
@@ -80,4 +88,18 @@ WHERE rk = 1
8088
ORDER BY student_id;
8189
```
8290

91+
```sql
92+
# Write your MySQL query statement below
93+
SELECT student_id, min(course_id) AS course_id, grade
94+
FROM Enrollments
95+
WHERE
96+
(student_id, grade) IN (
97+
SELECT student_id, max(grade) AS grade
98+
FROM Enrollments
99+
GROUP BY 1
100+
)
101+
GROUP BY 1
102+
ORDER BY 1;
103+
```
104+
83105
<!-- tabs:end -->

solution/1100-1199/1112.Highest Grade For Each Student/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Enrollments table:
5555

5656
## Solutions
5757

58+
**Solution 1: RANK() OVER() Window Function**
59+
60+
We can use the `RANK() OVER()` window function to sort the grades of each student in descending order. If the grades are the same, we sort them in ascending order by course number, and then select the record with a rank of $1$ for each student.
61+
62+
**Solution 2: Subquery**
63+
64+
We can first query the highest grade of each student, and then query the minimum course number corresponding to the highest grade of each student.
65+
5866
<!-- tabs:start -->
5967

6068
### **SQL**
@@ -77,4 +85,18 @@ WHERE rk = 1
7785
ORDER BY student_id;
7886
```
7987

88+
```sql
89+
# Write your MySQL query statement below
90+
SELECT student_id, min(course_id) AS course_id, grade
91+
FROM Enrollments
92+
WHERE
93+
(student_id, grade) IN (
94+
SELECT student_id, max(grade) AS grade
95+
FROM Enrollments
96+
GROUP BY 1
97+
)
98+
GROUP BY 1
99+
ORDER BY 1;
100+
```
101+
80102
<!-- tabs:end -->

solution/1400-1499/1407.Top Travellers/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Donald 没有任何行程, 他的旅行距离为 0。
9797

9898
<!-- 这里可写通用的实现逻辑 -->
9999

100+
**方法一:左连接 + 分组统计**
101+
102+
我们可以使用左连接,将 `Users` 表与 `Rides` 表按照用户 id 连接,然后按照用户 id 分组,统计每个用户的旅行距离。注意,如果用户没有旅行记录,那么旅行距离为 $0$。
103+
100104
<!-- tabs:start -->
101105

102106
### **SQL**

solution/1400-1499/1407.Top Travellers/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ Donald did not have any rides, the distance traveled by him is 0.
9292

9393
## Solutions
9494

95+
**Solution 1: LEFT JOIN + GROUP BY**
96+
97+
We can use a left join to join the `Users` table with the `Rides` table on the condition of user id, and then group by user id to calculate the travel distance for each user. Note that if a user has no travel records, the travel distance is $0$.
98+
9599
<!-- tabs:start -->
96100

97101
### **SQL**

solution/1600-1699/1607.Sellers With No Sales/README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,23 @@ Frank 在 2019 年卖出 1 次, 在 2020 年没有卖出。</pre>
108108

109109
<!-- 这里可写通用的实现逻辑 -->
110110

111+
**方法一:左连接 + 分组 + 筛选**
112+
113+
我们可以使用左连接,将 `Seller` 表与 `Orders` 表按照字段 `seller_id` 连接,然后按照 `seller_id` 分组,统计每个卖家在 $2020$ 年的卖出次数,最后筛选出卖出次数为 $0$ 的卖家。
114+
111115
<!-- tabs:start -->
112116

113117
### **SQL**
114118

115119
```sql
116120
# Write your MySQL query statement below
117-
SELECT
118-
seller_name
121+
SELECT seller_name
119122
FROM
120-
seller AS s
121-
LEFT JOIN orders AS o ON s.seller_id = o.seller_id AND year(sale_date) = '2020'
122-
WHERE o.seller_id IS NULL
123-
ORDER BY seller_name;
123+
Seller
124+
LEFT JOIN Orders USING (seller_id)
125+
GROUP BY seller_id
126+
HAVING ifnull(sum(year(sale_date) = 2020), 0) = 0
127+
ORDER BY 1;
124128
```
125129

126130
<!-- tabs:end -->

solution/1600-1699/1607.Sellers With No Sales/README_EN.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,23 @@ Frank made 1 sale in 2019 but no sales in 2020.
104104

105105
## Solutions
106106

107+
**Solution 1: LEFT JOIN + GROUP BY + FILTER**
108+
109+
We can use a left join to join the `Seller` table with the `Orders` table on the condition `seller_id`, and then group by `seller_id` to count the number of sales for each seller in the year $2020$. Finally, we can filter out the sellers with zero sales.
110+
107111
<!-- tabs:start -->
108112

109113
### **SQL**
110114

111115
```sql
112116
# Write your MySQL query statement below
113-
SELECT
114-
seller_name
117+
SELECT seller_name
115118
FROM
116-
seller AS s
117-
LEFT JOIN orders AS o ON s.seller_id = o.seller_id AND year(sale_date) = '2020'
118-
WHERE o.seller_id IS NULL
119-
ORDER BY seller_name;
119+
Seller
120+
LEFT JOIN Orders USING (seller_id)
121+
GROUP BY seller_id
122+
HAVING ifnull(sum(year(sale_date) = 2020), 0) = 0
123+
ORDER BY 1;
120124
```
121125

122126
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
seller_name
2+
SELECT seller_name
43
FROM
5-
seller AS s
6-
LEFT JOIN orders AS o ON s.seller_id = o.seller_id AND year(sale_date) = '2020'
7-
WHERE o.seller_id IS NULL
8-
ORDER BY seller_name;
4+
Seller
5+
LEFT JOIN Orders USING (seller_id)
6+
GROUP BY seller_id
7+
HAVING ifnull(sum(year(sale_date) = 2020), 0) = 0
8+
ORDER BY 1;

0 commit comments

Comments
 (0)