Skip to content

feat: update solutions to lc problems #1788

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

Merged
merged 1 commit into from
Oct 12, 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
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ node_modules/
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
/solution/1400-1499/1454.Active Users/Solution.sql
/solution/1400-1499/1484.Group Sold Products By The Date/Solution.sql
/solution/1500-1599/1511.Customer Order Frequency/Solution.sql
/solution/1600-1699/1613.Find the Missing IDs/Solution.sql
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
Expand Down
2 changes: 2 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
"solution/1400-1499/1445.Apples & Oranges/Solution.sql",
"solution/1400-1499/1479.Sales by Day of the Week/Solution.sql",
"solution/1500-1599/1501.Countries You Can Safely Invest In/Solution.sql",
"solution/1500-1599/1511.Customer Order Frequency/Solution.sql",
"solution/1500-1599/1555.Bank Account Summary/Solution.sql",
"solution/1600-1699/1667.Fix Names in a Table/Solution.sql",
"solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql",
"solution/1700-1799/1777.Product's Price for Each Store/Solution.sql",
"solution/1900-1999/1934.Confirmation Rate/Solution.sql",
"solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql",
Expand Down
15 changes: 12 additions & 3 deletions solution/0100-0199/0182.Duplicate Emails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,24 @@ Person 表:

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

**方法一:分组统计**

我们可以使用 `GROUP BY` 语句,按照 `email` 字段进行分组,然后使用 `HAVING` 语句,筛选出现次数大于 $1$ 的 `email`。

**方法二:自连接**

我们可以使用自连接的方法,将 `Person` 表自身连接一次,然后筛选出 `id` 不同,但 `email` 相同的记录。

<!-- tabs:start -->

### **SQL**

```sql
SELECT Email
# Write your MySQL query statement below
SELECT email
FROM Person
GROUP BY Email
HAVING count(Email) > 1;
GROUP BY 1
HAVING count(1) > 1;
```

```sql
Expand Down
15 changes: 12 additions & 3 deletions solution/0100-0199/0182.Duplicate Emails/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,24 @@ Person table:

## Solutions

**Solution 1: Group By + Having**

We can use the `GROUP BY` statement to group the data by the `email` field, and then use the `HAVING` statement to filter out the `email` addresses that appear more than once.

**Solution 2: Self-Join**

We can use a self-join to join the `Person` table with itself, and then filter out the records where the `id` is different but the `email` is the same.

<!-- tabs:start -->

### **SQL**

```sql
SELECT Email
# Write your MySQL query statement below
SELECT email
FROM Person
GROUP BY Email
HAVING count(Email) > 1;
GROUP BY 1
HAVING count(1) > 1;
```

```sql
Expand Down
7 changes: 4 additions & 3 deletions solution/0100-0199/0182.Duplicate Emails/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SELECT Email
# Write your MySQL query statement below
SELECT email
FROM Person
GROUP BY Email
HAVING count(Email) > 1;
GROUP BY 1
HAVING count(1) > 1;
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ ActorDirector 表:

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

**方法一:使用 `GROUP BY` + `HAVING`**
**方法一:分组统计**

我们将 `ActorDirector` 表按照 `actor_id` 和 `director_id` 进行分组,然后使用 `HAVING` 过滤出合作次数大于等于 $3$ 次的组
我们可以使用 `GROUP BY` 语句,按照 `actor_id` 和 `director_id` 字段进行分组,然后使用 `HAVING` 语句,筛选出现次数大于等于 $3$ 的 `actor_id` 和 `director_id`

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ ActorDirector table:

## Solutions

Use `GROUP BY` & `HAVING`.
**Solution 1: Group By + Having**

We can use the `GROUP BY` statement to group the data by the `actor_id` and `director_id` fields, and then use the `HAVING` statement to filter out the `actor_id` and `director_id` that appear at least three times.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,24 @@ TVProgram</code> 表:

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

**方法一:等值连接 + 条件筛选**

我们可以先通过等值连接将两张表按照 `content_id` 字段连接起来,然后再通过条件筛选出在 $2020$ 年 $6$ 月份播放的儿童适宜电影。

<!-- tabs:start -->

### **SQL**

```sql
SELECT DISTINCT
title
# Write your MySQL query statement below
SELECT DISTINCT title
FROM
Content
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
TVProgram
JOIN Content USING (content_id)
WHERE
content_type = 'Movies'
date_format(program_date, '%Y%m') = '202006'
AND kids_content = 'Y'
AND program_date BETWEEN '2020-06-01' AND '2020-06-30';
```

```sql
SELECT DISTINCT
title
FROM
Content
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020);
AND content_type = 'Movies';
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,22 @@ Content table:

## Solutions

**Solution 1: Equi-Join + Conditional Filtering**

We can first use an equi-join to join the two tables based on the `content_id` field, and then use conditional filtering to select the child-friendly movies that were played in June 2020.

<!-- tabs:start -->

```sql
SELECT DISTINCT
title
# Write your MySQL query statement below
SELECT DISTINCT title
FROM
Content
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
TVProgram
JOIN Content USING (content_id)
WHERE
content_type = 'Movies'
date_format(program_date, '%Y%m') = '202006'
AND kids_content = 'Y'
AND program_date BETWEEN '2020-06-01' AND '2020-06-30';
```

```sql
SELECT DISTINCT
title
FROM
Content
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020);
AND content_type = 'Movies';
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
SELECT DISTINCT
title
# Write your MySQL query statement below
SELECT DISTINCT title
FROM
Content
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
TVProgram
JOIN Content USING (content_id)
WHERE
content_type = 'Movies'
date_format(program_date, '%Y%m') = '202006'
AND kids_content = 'Y'
AND program_date BETWEEN '2020-06-01' AND '2020-06-30';

SELECT DISTINCT
title
FROM
Content
INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id
WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020);
AND content_type = 'Movies';
23 changes: 13 additions & 10 deletions solution/1500-1599/1511.Customer Order Frequency/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,26 @@ Moustafa 在2020年6月花费了110 (10 * 2 + 45 * 2), 在7月花费了0.</pre>

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

**方法一:等值连接 + 分组求和**

我们可以使用 `JOIN` 语句,连接 `Orders` 和 `Product` 表,再连接 `Customers` 表,筛选出 `order_date` 在 $2020$ 年的记录,然后使用 `GROUP BY` 语句,按照 `customer_id` 分组,使用 `HAVING` 语句,筛选出 $6$ 月和 $7$ 月花费大于等于 $100$ 的客户。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
c.customer_id AS CUSTOMER_ID,
c.name AS NAME
SELECT customer_id, name
FROM
Customers c, Product p, Orders o
WHERE
c.customer_id = o.customer_id
AND p.product_id = o.product_id
GROUP BY c.customer_id
HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100
AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100;
Orders
JOIN Product USING (product_id)
JOIN Customers USING (customer_id)
WHERE year(order_date) = 2020
GROUP BY 1
HAVING
sum(if(month(order_date) = 6, quantity * price, 0)) >= 100
AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100;
```

<!-- tabs:end -->
23 changes: 13 additions & 10 deletions solution/1500-1599/1511.Customer Order Frequency/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,26 @@ Moustafa spent 110 (10 * 2 + 45 * 2) in June and 0 in July 2020.

## Solutions

**Solution 1: Join + Group By + Having**

We can use the `JOIN` statement to join the `Orders` table and the `Product` table, and then join the result with the `Customers` table. We can filter out the records where the `order_date` is not in the year $2020$, and then use the `GROUP BY` statement to group the data by `customer_id`. Finally, we can use the `HAVING` statement to filter out the customers whose spending in June and July is greater than or equal to $100$.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
c.customer_id AS CUSTOMER_ID,
c.name AS NAME
SELECT customer_id, name
FROM
Customers c, Product p, Orders o
WHERE
c.customer_id = o.customer_id
AND p.product_id = o.product_id
GROUP BY c.customer_id
HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100
AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100;
Orders
JOIN Product USING (product_id)
JOIN Customers USING (customer_id)
WHERE year(order_date) = 2020
GROUP BY 1
HAVING
sum(if(month(order_date) = 6, quantity * price, 0)) >= 100
AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100;
```

<!-- tabs:end -->
19 changes: 9 additions & 10 deletions solution/1500-1599/1511.Customer Order Frequency/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Write your MySQL query statement below
SELECT
c.customer_id AS CUSTOMER_ID,
c.name AS NAME
SELECT customer_id, name
FROM
Customers c, Product p, Orders o
WHERE
c.customer_id = o.customer_id
AND p.product_id = o.product_id
GROUP BY c.customer_id
HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100
AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100;
Orders
JOIN Product USING (product_id)
JOIN Customers USING (customer_id)
WHERE year(order_date) = 2020
GROUP BY 1
HAVING
sum(if(month(order_date) = 6, quantity * price, 0)) >= 100
AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100;
23 changes: 8 additions & 15 deletions solution/1500-1599/1587.Bank Account Summary II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,26 @@ Charlie 的余额为(6000 + 6000 - 4000) = 8000.

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

**方法一:等值连接 + 分组求和**

我们可以使用等值连接,将 `Users` 和 `Transactions` 表按照 `account` 列连接起来,然后按照 `account` 列分组求和,最后筛选出余额大于 $10000$ 的用户。

<!-- tabs:start -->

### **SQL**

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

```sql
SELECT
u.name,
SUM(t.amount) AS balance
FROM
users AS u
JOIN transactions AS t ON u.account = t.account
GROUP BY name
HAVING SUM(t.amount) > 10000;
```

```sql
# Write your MySQL query statement below
SELECT
name,
sum(amount) AS balance
FROM
Users AS u
LEFT JOIN Transactions AS t ON u.account = t.account
GROUP BY u.account
HAVING sum(amount) > 10000;
Users
JOIN Transactions USING (account)
GROUP BY account
HAVING balance > 10000;
```

<!-- tabs:end -->
23 changes: 8 additions & 15 deletions solution/1500-1599/1587.Bank Account Summary II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,24 @@ Charlie&#39;s balance is (6000 + 6000 - 4000) = 8000.

## Solutions

**Solution 1: Equi-Join + Group By + Sum**

We can use an equi-join to join the `Users` table and the `Transactions` table on the condition of `account`, and then group by `account` to calculate the balance for each account using the `SUM` function. Finally, we can filter out the users whose balance is less than or equal to $10000$.

<!-- tabs:start -->

### **SQL**

```sql
SELECT
u.name,
SUM(t.amount) AS balance
FROM
users AS u
JOIN transactions AS t ON u.account = t.account
GROUP BY name
HAVING SUM(t.amount) > 10000;
```

```sql
# Write your MySQL query statement below
SELECT
name,
sum(amount) AS balance
FROM
Users AS u
LEFT JOIN Transactions AS t ON u.account = t.account
GROUP BY u.account
HAVING sum(amount) > 10000;
Users
JOIN Transactions USING (account)
GROUP BY account
HAVING balance > 10000;
```

<!-- tabs:end -->
Loading