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.0183,1821,1873 #1779

Merged
merged 3 commits 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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ node_modules/
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
/solution/1800-1899/1873.Calculate Special Bonus/Solution.sql
/solution/2100-2199/2118.Build the Equation/Solution.sql
/solution/2100-2199/2153.The Number of Passengers in Each Bus II/Solution.sql
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ Orders table:

## Solutions

**Solution 1: NOT IN**

List all customer IDs of existing orders, and use `NOT IN` to find customers who are not in the list.

**Solution 2: LEFT JOIN**

Use `LEFT JOIN` to join the tables and return the data where `CustomerId` is `NULL`.

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Customers

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

**方法一:WHERE 子句**

我们可以直接使用 `WHERE` 子句来筛选出 `year` 为 `2021` 且 `revenue` 大于 $0$ 的客户。

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Thus only customers 1 and 4 have positive revenue in the year 2021.

## Solutions

**Solution 1: WHERE Clause**

We can directly use the `WHERE` clause to filter out the customers whose `year` is `2021` and `revenue` is greater than $0$.

<!-- tabs:start -->

### **SQL**
Expand Down
34 changes: 7 additions & 27 deletions solution/1800-1899/1873.Calculate Special Bonus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,18 @@ Employees 表:

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

**方法一:IF 语句 + ORDER BY 子句**

我们可以使用 `IF` 语句来判断奖金的计算方式,然后使用 `ORDER BY` 将结果按照 `employee_id` 排序。

<!-- tabs:start -->

### **SQL**

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

```sql
SELECT
employee_id,
CASE
WHEN employee_id % 2 = 0
OR LEFT(name, 1) = 'M' THEN 0
ELSE salary
END AS bonus
FROM
employees;
```

MySQL

```sql
# Write your MySQL query statement below
SELECT
employee_id,
IF(
Expand All @@ -93,19 +84,8 @@ SELECT
salary
) AS bonus
FROM
employees;
```

```sql
# Write your MySQL query statement below
SELECT
employee_id,
CASE
WHEN (employee_id % 2 = 1 AND NAME NOT LIKE "M%") THEN salary
ELSE 0
END AS bonus
FROM Employees
ORDER BY employee_id;
employees
ORDER BY 1;
```

<!-- tabs:end -->
34 changes: 7 additions & 27 deletions solution/1800-1899/1873.Calculate Special Bonus/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,16 @@ The rest of the employees get a 100% bonus.

## Solutions

<!-- tabs:start -->
**Solution 1: IF Statement + ORDER BY Clause**

### **SQL**
We can use the `IF` statement to determine the calculation method of the bonus, and then use `ORDER BY` to sort the results by `employee_id`.

```sql
SELECT
employee_id,
CASE
WHEN employee_id % 2 = 0
OR LEFT(name, 1) = 'M' THEN 0
ELSE salary
END AS bonus
FROM
employees;
```
<!-- tabs:start -->

MySQL
### **SQL**

```sql
# Write your MySQL query statement below
SELECT
employee_id,
IF(
Expand All @@ -87,19 +78,8 @@ SELECT
salary
) AS bonus
FROM
employees;
```

```sql
# Write your MySQL query statement below
SELECT
employee_id,
CASE
WHEN (employee_id % 2 = 1 AND NAME NOT LIKE "M%") THEN salary
ELSE 0
END AS bonus
FROM Employees
ORDER BY employee_id;
employees
ORDER BY 1;
```

<!-- tabs:end -->
9 changes: 0 additions & 9 deletions solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL

This file was deleted.

12 changes: 12 additions & 0 deletions solution/1800-1899/1873.Calculate Special Bonus/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Write your MySQL query statement below
SELECT
employee_id,
IF(
employee_id % 2 = 0
OR LEFT(name, 1) = 'M',
0,
salary
) AS bonus
FROM
employees
ORDER BY 1;