From fc3590c15f67b5e136d11cab0563acebf6572045 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 10 Oct 2023 14:49:27 +0800 Subject: [PATCH 1/3] feat: update sql solutions to lc problems: No.0183,1821,1873 * No.0183.Customers Who Never Order * No.1821.Find Customers With Positive Revenue this Year * No.1873.Calculate Special Bonus --- .../README_EN.md | 8 +++++ .../README.md | 4 +++ .../README_EN.md | 4 +++ .../1873.Calculate Special Bonus/README.md | 34 ++++--------------- .../1873.Calculate Special Bonus/README_EN.md | 34 ++++--------------- .../1873.Calculate Special Bonus/Solution.SQL | 21 +++++++----- 6 files changed, 42 insertions(+), 63 deletions(-) diff --git a/solution/0100-0199/0183.Customers Who Never Order/README_EN.md b/solution/0100-0199/0183.Customers Who Never Order/README_EN.md index b8b1ab9022201..748a9aaa0ad30 100644 --- a/solution/0100-0199/0183.Customers Who Never Order/README_EN.md +++ b/solution/0100-0199/0183.Customers Who Never Order/README_EN.md @@ -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`. + ### **SQL** diff --git a/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README.md b/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README.md index 97dcb56ed97ac..c70907487c719 100644 --- a/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README.md +++ b/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README.md @@ -65,6 +65,10 @@ Customers +**方法一:WHERE 子句** + +我们可以直接使用 `WHERE` 子句来筛选出 `year` 为 `2021` 且 `revenue` 大于 $0$ 的客户。 + ### **SQL** diff --git a/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README_EN.md b/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README_EN.md index d788908066c8e..4827aa76b7b9a 100644 --- a/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README_EN.md +++ b/solution/1800-1899/1821.Find Customers With Positive Revenue this Year/README_EN.md @@ -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$. + ### **SQL** diff --git a/solution/1800-1899/1873.Calculate Special Bonus/README.md b/solution/1800-1899/1873.Calculate Special Bonus/README.md index e073baf3e290c..f8f1dd2a5e8db 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/README.md +++ b/solution/1800-1899/1873.Calculate Special Bonus/README.md @@ -63,6 +63,10 @@ Employees 表: +**方法一:IF 语句 + ORDER BY 子句** + +我们可以使用 `IF` 语句来判断奖金的计算方式,然后使用 `ORDER BY` 将结果按照 `employee_id` 排序。 + ### **SQL** @@ -70,20 +74,7 @@ Employees 表: ```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( @@ -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; ``` diff --git a/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md b/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md index 5f7a3671f3efb..6c37b8363d137 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md +++ b/solution/1800-1899/1873.Calculate Special Bonus/README_EN.md @@ -59,25 +59,16 @@ The rest of the employees get a 100% bonus. ## Solutions - +**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; -``` + -MySQL +### **SQL** ```sql +# Write your MySQL query statement below SELECT employee_id, IF( @@ -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; ``` diff --git a/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL b/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL index b38b97327da59..abd8f66f3d8b3 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL +++ b/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL @@ -1,9 +1,12 @@ -SELECT - employee_id, - CASE - WHEN employee_id % 2 = 0 - OR LEFT(name, 1) = 'M' THEN 0 - ELSE salary - END AS bonus -FROM - employees; \ No newline at end of file +# 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; \ No newline at end of file From 96a1f21384337bf28eaa73b11d8cb354da8a8e62 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 10 Oct 2023 14:51:58 +0800 Subject: [PATCH 2/3] Update and rename Solution.SQL to Solution.sql --- .../1873.Calculate Special Bonus/{Solution.SQL => Solution.sql} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename solution/1800-1899/1873.Calculate Special Bonus/{Solution.SQL => Solution.sql} (88%) diff --git a/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL b/solution/1800-1899/1873.Calculate Special Bonus/Solution.sql similarity index 88% rename from solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL rename to solution/1800-1899/1873.Calculate Special Bonus/Solution.sql index abd8f66f3d8b3..beb80a26a2ef5 100644 --- a/solution/1800-1899/1873.Calculate Special Bonus/Solution.SQL +++ b/solution/1800-1899/1873.Calculate Special Bonus/Solution.sql @@ -9,4 +9,4 @@ SELECT ) AS bonus FROM employees -ORDER BY 1; \ No newline at end of file +ORDER BY 1; From 6a4d42b1ee5e0a141b38deb622f8e2a4d27e285f Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Tue, 10 Oct 2023 07:49:14 +0000 Subject: [PATCH 3/3] fix: ignore file --- .prettierignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.prettierignore b/.prettierignore index 1eb3d9c657ff6..92592c9d189ab 100644 --- a/.prettierignore +++ b/.prettierignore @@ -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