Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update sql solutions to lc problems: No.0607,1112,1407,1607 #1781

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions solution/0100-0199/0175.Combine Two Tables/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ addressId = 1 contains information about the address of personId = 2.

## Solutions

**Solution 1: LEFT JOIN**

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`.

<!-- tabs:start -->

### **SQL**
Expand Down
51 changes: 11 additions & 40 deletions solution/0600-0699/0607.Sales Person/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,52 +116,23 @@ Orders 表:

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

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

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

```sql
SELECT name
FROM salesperson
WHERE
sales_id NOT IN (
SELECT s.sales_id
FROM
orders AS o
INNER JOIN salesperson AS s ON o.sales_id = s.sales_id
INNER JOIN company AS c ON o.com_id = c.com_id
WHERE c.name = 'RED'
);
```
<!-- tabs:start -->

```sql
SELECT
name
FROM SalesPerson AS s
WHERE
0 = (
SELECT
COUNT(*)
FROM
Orders AS o
JOIN Company AS c ON o.com_id = c.com_id
WHERE o.sales_id = s.sales_id AND c.name = 'RED'
);
```
### **SQL**

```sql
# Write your MySQL query statement below
SELECT name
FROM SalesPerson
WHERE
sales_id NOT IN (
SELECT sales_id
FROM
Company AS c
JOIN Orders AS o USING (com_id)
GROUP BY sales_id
HAVING sum(name = 'RED') > 0
);
SELECT s.name
FROM
SalesPerson AS s
LEFT JOIN Orders USING (sales_id)
LEFT JOIN Company AS c USING (com_id)
GROUP BY sales_id
HAVING ifnull(sum(c.name = 'RED'), 0) = 0;
```

<!-- tabs:end -->
51 changes: 11 additions & 40 deletions solution/0600-0699/0607.Sales Person/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,52 +111,23 @@ According to orders 3 and 4 in the Orders table, it is easy to tell that only sa

## Solutions

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

### **SQL**
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`.

```sql
SELECT name
FROM salesperson
WHERE
sales_id NOT IN (
SELECT s.sales_id
FROM
orders AS o
INNER JOIN salesperson AS s ON o.sales_id = s.sales_id
INNER JOIN company AS c ON o.com_id = c.com_id
WHERE c.name = 'RED'
);
```
<!-- tabs:start -->

```sql
SELECT
name
FROM SalesPerson AS s
WHERE
0 = (
SELECT
COUNT(*)
FROM
Orders AS o
JOIN Company AS c ON o.com_id = c.com_id
WHERE o.sales_id = s.sales_id AND c.name = 'RED'
);
```
### **SQL**

```sql
# Write your MySQL query statement below
SELECT name
FROM SalesPerson
WHERE
sales_id NOT IN (
SELECT sales_id
FROM
Company AS c
JOIN Orders AS o USING (com_id)
GROUP BY sales_id
HAVING sum(name = 'RED') > 0
);
SELECT s.name
FROM
SalesPerson AS s
LEFT JOIN Orders USING (sales_id)
LEFT JOIN Company AS c USING (com_id)
GROUP BY sales_id
HAVING ifnull(sum(c.name = 'RED'), 0) = 0;
```

<!-- tabs:end -->
19 changes: 8 additions & 11 deletions solution/0600-0699/0607.Sales Person/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
SELECT name
FROM salesperson
WHERE
sales_id NOT IN (
SELECT s.sales_id
FROM
orders AS o
INNER JOIN salesperson AS s ON o.sales_id = s.sales_id
INNER JOIN company AS c ON o.com_id = c.com_id
WHERE c.name = 'RED'
);
# Write your MySQL query statement below
SELECT s.name
FROM
SalesPerson AS s
LEFT JOIN Orders USING (sales_id)
LEFT JOIN Company AS c USING (com_id)
GROUP BY sales_id
HAVING ifnull(sum(c.name = 'RED'), 0) = 0;
22 changes: 22 additions & 0 deletions solution/1100-1199/1112.Highest Grade For Each Student/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ Enrollments 表:

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

**方法一:RANK() OVER() 窗口函数**

我们可以使用 `RANK() OVER()` 窗口函数,按照每个学生的成绩降序排列,如果成绩相同,按照课程号升序排列,然后取每个学生排名为 $1$ 的记录。

**方法二:子查询**

我们可以先查询每个学生的最高成绩,然后再查询每个学生的最高成绩对应的最小课程号。

<!-- tabs:start -->

### **SQL**
Expand All @@ -80,4 +88,18 @@ WHERE rk = 1
ORDER BY student_id;
```

```sql
# Write your MySQL query statement below
SELECT student_id, min(course_id) AS course_id, grade
FROM Enrollments
WHERE
(student_id, grade) IN (
SELECT student_id, max(grade) AS grade
FROM Enrollments
GROUP BY 1
)
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ Enrollments table:

## Solutions

**Solution 1: RANK() OVER() Window Function**

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.

**Solution 2: Subquery**

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.

<!-- tabs:start -->

### **SQL**
Expand All @@ -77,4 +85,18 @@ WHERE rk = 1
ORDER BY student_id;
```

```sql
# Write your MySQL query statement below
SELECT student_id, min(course_id) AS course_id, grade
FROM Enrollments
WHERE
(student_id, grade) IN (
SELECT student_id, max(grade) AS grade
FROM Enrollments
GROUP BY 1
)
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->
4 changes: 4 additions & 0 deletions solution/1400-1499/1407.Top Travellers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ Donald 没有任何行程, 他的旅行距离为 0。

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

**方法一:左连接 + 分组统计**

我们可以使用左连接,将 `Users` 表与 `Rides` 表按照用户 id 连接,然后按照用户 id 分组,统计每个用户的旅行距离。注意,如果用户没有旅行记录,那么旅行距离为 $0$。

<!-- tabs:start -->

### **SQL**
Expand Down
4 changes: 4 additions & 0 deletions solution/1400-1499/1407.Top Travellers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ Donald did not have any rides, the distance traveled by him is 0.

## Solutions

**Solution 1: LEFT JOIN + GROUP BY**

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$.

<!-- tabs:start -->

### **SQL**
Expand Down
16 changes: 10 additions & 6 deletions solution/1600-1699/1607.Sellers With No Sales/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,23 @@ Frank 在 2019 年卖出 1 次, 在 2020 年没有卖出。</pre>

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

**方法一:左连接 + 分组 + 筛选**

我们可以使用左连接,将 `Seller` 表与 `Orders` 表按照字段 `seller_id` 连接,然后按照 `seller_id` 分组,统计每个卖家在 $2020$ 年的卖出次数,最后筛选出卖出次数为 $0$ 的卖家。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
seller_name
SELECT seller_name
FROM
seller AS s
LEFT JOIN orders AS o ON s.seller_id = o.seller_id AND year(sale_date) = '2020'
WHERE o.seller_id IS NULL
ORDER BY seller_name;
Seller
LEFT JOIN Orders USING (seller_id)
GROUP BY seller_id
HAVING ifnull(sum(year(sale_date) = 2020), 0) = 0
ORDER BY 1;
```

<!-- tabs:end -->
16 changes: 10 additions & 6 deletions solution/1600-1699/1607.Sellers With No Sales/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,23 @@ Frank made 1 sale in 2019 but no sales in 2020.

## Solutions

**Solution 1: LEFT JOIN + GROUP BY + FILTER**

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.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
seller_name
SELECT seller_name
FROM
seller AS s
LEFT JOIN orders AS o ON s.seller_id = o.seller_id AND year(sale_date) = '2020'
WHERE o.seller_id IS NULL
ORDER BY seller_name;
Seller
LEFT JOIN Orders USING (seller_id)
GROUP BY seller_id
HAVING ifnull(sum(year(sale_date) = 2020), 0) = 0
ORDER BY 1;
```

<!-- tabs:end -->
12 changes: 6 additions & 6 deletions solution/1600-1699/1607.Sellers With No Sales/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Write your MySQL query statement below
SELECT
seller_name
SELECT seller_name
FROM
seller AS s
LEFT JOIN orders AS o ON s.seller_id = o.seller_id AND year(sale_date) = '2020'
WHERE o.seller_id IS NULL
ORDER BY seller_name;
Seller
LEFT JOIN Orders USING (seller_id)
GROUP BY seller_id
HAVING ifnull(sum(year(sale_date) = 2020), 0) = 0
ORDER BY 1;