diff --git a/solution/0500-0599/0577.Employee Bonus/README.md b/solution/0500-0599/0577.Employee Bonus/README.md index b333b8871cfa3..dedfd49c5851b 100644 --- a/solution/0500-0599/0577.Employee Bonus/README.md +++ b/solution/0500-0599/0577.Employee Bonus/README.md @@ -52,13 +52,12 @@ empId 是这张表单的主关键字 ### **SQL** ```sql -SELECT - e.name, - b.bonus +# Write your MySQL query statement below +SELECT name, bonus FROM - Employee AS e - LEFT JOIN Bonus AS b ON e.empid = b.empid -WHERE b.bonus < 1000 OR b.bonus IS NULL; + Employee + LEFT JOIN Bonus USING (empId) +WHERE ifnull(bonus, 0) < 1000; ``` diff --git a/solution/0500-0599/0577.Employee Bonus/README_EN.md b/solution/0500-0599/0577.Employee Bonus/README_EN.md index fa76a90a66961..1f578b0b46594 100644 --- a/solution/0500-0599/0577.Employee Bonus/README_EN.md +++ b/solution/0500-0599/0577.Employee Bonus/README_EN.md @@ -81,13 +81,12 @@ Bonus table: ### **SQL** ```sql -SELECT - e.name, - b.bonus +# Write your MySQL query statement below +SELECT name, bonus FROM - Employee AS e - LEFT JOIN Bonus AS b ON e.empid = b.empid -WHERE b.bonus < 1000 OR b.bonus IS NULL; + Employee + LEFT JOIN Bonus USING (empId) +WHERE ifnull(bonus, 0) < 1000; ``` diff --git a/solution/0500-0599/0577.Employee Bonus/Solution.sql b/solution/0500-0599/0577.Employee Bonus/Solution.sql index 972bc7f047f63..e4e860f4f1b3d 100644 --- a/solution/0500-0599/0577.Employee Bonus/Solution.sql +++ b/solution/0500-0599/0577.Employee Bonus/Solution.sql @@ -1,7 +1,6 @@ -SELECT - e.name, - b.bonus +# Write your MySQL query statement below +SELECT name, bonus FROM - Employee AS e - LEFT JOIN Bonus AS b ON e.empid = b.empid -WHERE b.bonus < 1000 OR b.bonus IS NULL; + Employee + LEFT JOIN Bonus USING (empId) +WHERE ifnull(bonus, 0) < 1000; diff --git a/solution/0500-0599/0578.Get Highest Answer Rate Question/README.md b/solution/0500-0599/0578.Get Highest Answer Rate Question/README.md index 800a622062c50..e2ee1c2fd5c89 100644 --- a/solution/0500-0599/0578.Get Highest Answer Rate Question/README.md +++ b/solution/0500-0599/0578.Get Highest Answer Rate Question/README.md @@ -73,11 +73,28 @@ SurveyLog table: ### **SQL** ```sql +# Write your MySQL query statement below SELECT question_id AS survey_log -FROM SurveyLog -GROUP BY 1 -ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC -LIMIT 1; +FROM SurveyLog +GROUP BY 1 +ORDER BY sum(action = 'answer') / sum(action = 'show') DESC, 1 +LIMIT 1; +``` + +```sql +WITH + T AS ( + SELECT + question_id AS survey_log, + (sum(action = 'answer') OVER (PARTITION BY question_id)) / ( + sum(action = 'show') OVER (PARTITION BY question_id) + ) AS ratio + FROM SurveyLog + ) +SELECT survey_log +FROM T +ORDER BY ratio DESC, 1 +LIMIT 1; ``` diff --git a/solution/0500-0599/0578.Get Highest Answer Rate Question/README_EN.md b/solution/0500-0599/0578.Get Highest Answer Rate Question/README_EN.md index 4fe900b6a10ee..4657f854ad793 100644 --- a/solution/0500-0599/0578.Get Highest Answer Rate Question/README_EN.md +++ b/solution/0500-0599/0578.Get Highest Answer Rate Question/README_EN.md @@ -64,11 +64,28 @@ Question 285 has the highest answer rate. ### **SQL** ```sql +# Write your MySQL query statement below SELECT question_id AS survey_log -FROM SurveyLog -GROUP BY 1 -ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC -LIMIT 1; +FROM SurveyLog +GROUP BY 1 +ORDER BY sum(action = 'answer') / sum(action = 'show') DESC, 1 +LIMIT 1; +``` + +```sql +WITH + T AS ( + SELECT + question_id AS survey_log, + (sum(action = 'answer') OVER (PARTITION BY question_id)) / ( + sum(action = 'show') OVER (PARTITION BY question_id) + ) AS ratio + FROM SurveyLog + ) +SELECT survey_log +FROM T +ORDER BY ratio DESC, 1 +LIMIT 1; ``` diff --git a/solution/0500-0599/0578.Get Highest Answer Rate Question/Solution.sql b/solution/0500-0599/0578.Get Highest Answer Rate Question/Solution.sql index 5d28ac5669276..fe090d0873f2f 100644 --- a/solution/0500-0599/0578.Get Highest Answer Rate Question/Solution.sql +++ b/solution/0500-0599/0578.Get Highest Answer Rate Question/Solution.sql @@ -1,5 +1,13 @@ -SELECT question_id AS survey_log -FROM SurveyLog -GROUP BY 1 -ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC +WITH + T AS ( + SELECT + question_id AS survey_log, + (sum(action = 'answer') OVER (PARTITION BY question_id)) / ( + sum(action = 'show') OVER (PARTITION BY question_id) + ) AS ratio + FROM SurveyLog + ) +SELECT survey_log +FROM T +ORDER BY ratio DESC, 1 LIMIT 1; diff --git a/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README.md b/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README.md index ed297ea76c49e..b8ba284be6cea 100644 --- a/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README.md +++ b/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README.md @@ -109,4 +109,28 @@ WHERE ORDER BY id, month DESC; ``` +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + id, + month, + sum(salary) OVER ( + PARTITION BY id + ORDER BY month + RANGE 2 PRECEDING + ) AS salary, + rank() OVER ( + PARTITION BY id + ORDER BY month DESC + ) AS rk + FROM Employee + ) +SELECT id, month, salary +FROM T +WHERE rk > 1 +ORDER BY 1, 2 DESC; +``` + diff --git a/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README_EN.md b/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README_EN.md index 7bf3d9dc2bacd..671c04dd6a486 100644 --- a/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README_EN.md +++ b/solution/0500-0599/0579.Find Cumulative Salary of an Employee/README_EN.md @@ -134,4 +134,28 @@ WHERE ORDER BY id, month DESC; ``` +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + id, + month, + sum(salary) OVER ( + PARTITION BY id + ORDER BY month + RANGE 2 PRECEDING + ) AS salary, + rank() OVER ( + PARTITION BY id + ORDER BY month DESC + ) AS rk + FROM Employee + ) +SELECT id, month, salary +FROM T +WHERE rk > 1 +ORDER BY 1, 2 DESC; +``` + diff --git a/solution/0500-0599/0579.Find Cumulative Salary of an Employee/Solution.sql b/solution/0500-0599/0579.Find Cumulative Salary of an Employee/Solution.sql index c02eda0456e41..35dd2db482c31 100644 --- a/solution/0500-0599/0579.Find Cumulative Salary of an Employee/Solution.sql +++ b/solution/0500-0599/0579.Find Cumulative Salary of an Employee/Solution.sql @@ -1,19 +1,21 @@ # Write your MySQL query statement below -SELECT - id, - month, - sum(salary) OVER ( - PARTITION BY id - ORDER BY month - RANGE 2 PRECEDING - ) AS Salary -FROM employee -WHERE - (id, month) NOT IN ( +WITH + T AS ( SELECT id, - max(month) + month, + sum(salary) OVER ( + PARTITION BY id + ORDER BY month + RANGE 2 PRECEDING + ) AS salary, + rank() OVER ( + PARTITION BY id + ORDER BY month DESC + ) AS rk FROM Employee - GROUP BY id ) -ORDER BY id, month DESC; +SELECT id, month, salary +FROM T +WHERE rk > 1 +ORDER BY 1, 2 DESC; diff --git a/solution/0500-0599/0580.Count Student Number in Departments/README.md b/solution/0500-0599/0580.Count Student Number in Departments/README.md index 409d6757036fa..711c46cc60665 100644 --- a/solution/0500-0599/0580.Count Student Number in Departments/README.md +++ b/solution/0500-0599/0580.Count Student Number in Departments/README.md @@ -84,14 +84,18 @@ Department 表: ### **SQL** ```sql -SELECT - department.dept_name, - COUNT(student.dept_id) AS student_number +# Write your MySQL query statement below +WITH + S AS ( + SELECT dept_id, count(1) AS cnt + FROM Student + GROUP BY dept_id + ) +SELECT dept_name, ifnull(cnt, 0) AS student_number FROM - Student - RIGHT JOIN Department ON student.dept_id = department.dept_id -GROUP BY dept_name -ORDER BY student_number DESC, dept_name; + S + RIGHT JOIN Department USING (dept_id) +ORDER BY 2 DESC, 1; ``` diff --git a/solution/0500-0599/0580.Count Student Number in Departments/README_EN.md b/solution/0500-0599/0580.Count Student Number in Departments/README_EN.md index 670e3b4c71cfa..8ac386319bed8 100644 --- a/solution/0500-0599/0580.Count Student Number in Departments/README_EN.md +++ b/solution/0500-0599/0580.Count Student Number in Departments/README_EN.md @@ -81,14 +81,18 @@ Department table: ### **SQL** ```sql -SELECT - department.dept_name, - COUNT(student.dept_id) AS student_number +# Write your MySQL query statement below +WITH + S AS ( + SELECT dept_id, count(1) AS cnt + FROM Student + GROUP BY dept_id + ) +SELECT dept_name, ifnull(cnt, 0) AS student_number FROM - Student - RIGHT JOIN Department ON student.dept_id = department.dept_id -GROUP BY dept_name -ORDER BY student_number DESC, dept_name; + S + RIGHT JOIN Department USING (dept_id) +ORDER BY 2 DESC, 1; ``` diff --git a/solution/0500-0599/0580.Count Student Number in Departments/Solution.sql b/solution/0500-0599/0580.Count Student Number in Departments/Solution.sql index 724a8e1391c0f..0cf0bdc6003eb 100644 --- a/solution/0500-0599/0580.Count Student Number in Departments/Solution.sql +++ b/solution/0500-0599/0580.Count Student Number in Departments/Solution.sql @@ -1,8 +1,12 @@ -SELECT - department.dept_name, - COUNT(student.dept_id) AS student_number +# Write your MySQL query statement below +WITH + S AS ( + SELECT dept_id, count(1) AS cnt + FROM Student + GROUP BY dept_id + ) +SELECT dept_name, ifnull(cnt, 0) AS student_number FROM - Student - RIGHT JOIN Department ON student.dept_id = department.dept_id -GROUP BY dept_name -ORDER BY student_number DESC, dept_name; + S + RIGHT JOIN Department USING (dept_id) +ORDER BY 2 DESC, 1; diff --git a/solution/0500-0599/0584.Find Customer Referee/README.md b/solution/0500-0599/0584.Find Customer Referee/README.md index ca484c3fbeabf..f33a73b4e0f1b 100644 --- a/solution/0500-0599/0584.Find Customer Referee/README.md +++ b/solution/0500-0599/0584.Find Customer Referee/README.md @@ -45,19 +45,10 @@ ### **SQL** ```sql -SELECT - name +# Write your MySQL query statement below +SELECT name FROM Customer -WHERE referee_id != 2 OR referee_id IS NULL; -``` - -MySQL 可使用 `IFNULL()`: - -```sql -SELECT - name -FROM customer -WHERE IFNULL(referee_id, 0) != 2; +WHERE ifnull(referee_id, 0) != 2; ``` diff --git a/solution/0500-0599/0584.Find Customer Referee/README_EN.md b/solution/0500-0599/0584.Find Customer Referee/README_EN.md index e2a2526f9b5d4..cbf93c6a88806 100644 --- a/solution/0500-0599/0584.Find Customer Referee/README_EN.md +++ b/solution/0500-0599/0584.Find Customer Referee/README_EN.md @@ -60,19 +60,10 @@ Customer table: ### **SQL** ```sql -SELECT - name +# Write your MySQL query statement below +SELECT name FROM Customer -WHERE referee_id != 2 OR referee_id IS NULL; -``` - -MySQL can use `IFNULL()`: - -```sql -SELECT - name -FROM customer -WHERE IFNULL(referee_id, 0) != 2; +WHERE ifnull(referee_id, 0) != 2; ``` diff --git a/solution/0500-0599/0584.Find Customer Referee/Solution.sql b/solution/0500-0599/0584.Find Customer Referee/Solution.sql index f4d2219f32e80..f4511718b3518 100644 --- a/solution/0500-0599/0584.Find Customer Referee/Solution.sql +++ b/solution/0500-0599/0584.Find Customer Referee/Solution.sql @@ -1,4 +1,4 @@ -SELECT - name +# Write your MySQL query statement below +SELECT name FROM Customer -WHERE referee_id != 2 OR referee_id IS NULL; +WHERE ifnull(referee_id, 0) != 2;