diff --git a/solution/0000-0099/0016.3Sum Closest/README.md b/solution/0000-0099/0016.3Sum Closest/README.md index e368442e87114..8fd2fae381f91 100644 --- a/solution/0000-0099/0016.3Sum Closest/README.md +++ b/solution/0000-0099/0016.3Sum Closest/README.md @@ -45,7 +45,7 @@ **方法一:排序 + 双指针** -将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。 +我们将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。 时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。 diff --git a/solution/0500-0599/0585.Investments in 2016/README.md b/solution/0500-0599/0585.Investments in 2016/README.md index 59b4ff0d0843c..745a27488505c 100644 --- a/solution/0500-0599/0585.Investments in 2016/README.md +++ b/solution/0500-0599/0585.Investments in 2016/README.md @@ -80,17 +80,16 @@ tiv_2015 值为 10 与第三条和第四条记录相同,且其位置是唯一 ```sql # Write your MySQL query statement below WITH - t AS ( + T AS ( SELECT tiv_2016, count(pid) OVER (PARTITION BY tiv_2015) AS cnt1, - count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2 + count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2 FROM Insurance ) -SELECT - round(sum(TIV_2016), 2) AS tiv_2016 -FROM t -WHERE cnt1 != 1 AND cnt2 = 1; +SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016 +FROM T +WHERE cnt1 > 1 AND cnt2 = 1; ``` diff --git a/solution/0500-0599/0585.Investments in 2016/README_EN.md b/solution/0500-0599/0585.Investments in 2016/README_EN.md index 6f8bc89abde88..c39e80d62e4a5 100644 --- a/solution/0500-0599/0585.Investments in 2016/README_EN.md +++ b/solution/0500-0599/0585.Investments in 2016/README_EN.md @@ -74,17 +74,16 @@ So, the result is the sum of tiv_2016 of the first and last record, which is 45. ```sql # Write your MySQL query statement below WITH - t AS ( + T AS ( SELECT tiv_2016, count(pid) OVER (PARTITION BY tiv_2015) AS cnt1, - count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2 + count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2 FROM Insurance ) -SELECT - round(sum(TIV_2016), 2) AS tiv_2016 -FROM t -WHERE cnt1 != 1 AND cnt2 = 1; +SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016 +FROM T +WHERE cnt1 > 1 AND cnt2 = 1; ``` diff --git a/solution/0500-0599/0585.Investments in 2016/Solution.sql b/solution/0500-0599/0585.Investments in 2016/Solution.sql index 4f3d3368aec24..dc5894796dce3 100644 --- a/solution/0500-0599/0585.Investments in 2016/Solution.sql +++ b/solution/0500-0599/0585.Investments in 2016/Solution.sql @@ -1,13 +1,12 @@ # Write your MySQL query statement below WITH - t AS ( + T AS ( SELECT tiv_2016, count(pid) OVER (PARTITION BY tiv_2015) AS cnt1, - count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2 + count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2 FROM Insurance ) -SELECT - round(sum(TIV_2016), 2) AS tiv_2016 -FROM t -WHERE cnt1 != 1 AND cnt2 = 1; +SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016 +FROM T +WHERE cnt1 > 1 AND cnt2 = 1; diff --git a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md index 5d3840d57e0d6..918e1b3a99063 100644 --- a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md +++ b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md @@ -75,9 +75,8 @@ ORDER BY count(1) DESC LIMIT 1; ``` -SQL Server - ```sql +/* Write your T-SQL query statement below */ SELECT TOP 1 customer_number FROM diff --git a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md index 05107a01021af..fa7d073b7e999 100644 --- a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md +++ b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md @@ -61,17 +61,15 @@ So the result is customer_number 3. ```sql # Write your MySQL query statement below -SELECT - customer_number -FROM orders -GROUP BY customer_number +SELECT customer_number +FROM Orders +GROUP BY 1 ORDER BY count(1) DESC LIMIT 1; ``` -SQL Server - ```sql +/* Write your T-SQL query statement below */ SELECT TOP 1 customer_number FROM diff --git a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution.sql b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution.sql index 29d5e261076ab..7bfa331b10f11 100644 --- a/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution.sql +++ b/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution.sql @@ -1,7 +1,6 @@ # Write your MySQL query statement below -SELECT - customer_number -FROM orders -GROUP BY customer_number +SELECT customer_number +FROM Orders +GROUP BY 1 ORDER BY count(1) DESC LIMIT 1; diff --git a/solution/0500-0599/0595.Big Countries/README.md b/solution/0500-0599/0595.Big Countries/README.md index a59f197e24c09..ae2e25a307eca 100644 --- a/solution/0500-0599/0595.Big Countries/README.md +++ b/solution/0500-0599/0595.Big Countries/README.md @@ -72,17 +72,34 @@ World 表: +**方法一:使用 WHERE + OR** + +我们可以使用 `WHERE` + `OR` 查询出所有符合条件的国家。 + +**方法二:使用 UNION** + +我们可以查询出所有面积大于等于 300 万平方公里的国家,然后再查询出所有人口大于等于 2500 万的国家,最后使用 `UNION` 将两个结果集合并起来。 + ### **SQL** ```sql -SELECT - name, - population, - area -FROM world -WHERE area > 3000000 OR population > 25000000; +# Write your MySQL query statement below +SELECT name, population, area +FROM World +WHERE area >= 3000000 OR population >= 25000000; +``` + +```sql +# Write your MySQL query statement below +SELECT name, population, area +FROM World +WHERE area >= 3000000 +UNION +SELECT name, population, area +FROM World +WHERE population >= 25000000; ``` diff --git a/solution/0500-0599/0595.Big Countries/README_EN.md b/solution/0500-0599/0595.Big Countries/README_EN.md index b1ea8c6f17dad..d152e032c268a 100644 --- a/solution/0500-0599/0595.Big Countries/README_EN.md +++ b/solution/0500-0599/0595.Big Countries/README_EN.md @@ -66,12 +66,21 @@ World table: ### **SQL** ```sql -SELECT - name, - population, - area -FROM world -WHERE area > 3000000 OR population > 25000000; +# Write your MySQL query statement below +SELECT name, population, area +FROM World +WHERE area >= 3000000 OR population >= 25000000; +``` + +```sql +# Write your MySQL query statement below +SELECT name, population, area +FROM World +WHERE area >= 3000000 +UNION +SELECT name, population, area +FROM World +WHERE population >= 25000000; ``` diff --git a/solution/0500-0599/0595.Big Countries/Solution.sql b/solution/0500-0599/0595.Big Countries/Solution.sql index 270a6e80cd819..bd3485c0d5753 100644 --- a/solution/0500-0599/0595.Big Countries/Solution.sql +++ b/solution/0500-0599/0595.Big Countries/Solution.sql @@ -1,6 +1,4 @@ -SELECT - name, - population, - area -FROM world -WHERE area > 3000000 OR population > 25000000; +# Write your MySQL query statement below +SELECT name, population, area +FROM World +WHERE area >= 3000000 OR population >= 25000000; diff --git a/solution/0500-0599/0596.Classes More Than 5 Students/README.md b/solution/0500-0599/0596.Classes More Than 5 Students/README.md index cc38a9d670b13..21bfa79ea2a47 100644 --- a/solution/0500-0599/0596.Classes More Than 5 Students/README.md +++ b/solution/0500-0599/0596.Classes More Than 5 Students/README.md @@ -68,11 +68,11 @@ Courses table: ### **SQL** ```sql -SELECT - class -FROM courses +# Write your MySQL query statement below +SELECT class +FROM Courses GROUP BY class -HAVING COUNT(class) >= 5; +HAVING count(1) >= 5; ``` diff --git a/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md b/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md index 1ef2a1404ff97..4a1c305857fe3 100644 --- a/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md +++ b/solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md @@ -64,11 +64,11 @@ Courses table: ### **SQL** ```sql -SELECT - class -FROM courses +# Write your MySQL query statement below +SELECT class +FROM Courses GROUP BY class -HAVING COUNT(class) >= 5; +HAVING count(1) >= 5; ``` diff --git a/solution/0500-0599/0596.Classes More Than 5 Students/Solution.sql b/solution/0500-0599/0596.Classes More Than 5 Students/Solution.sql index d0d92b9ef9969..f1e77df3a05b2 100644 --- a/solution/0500-0599/0596.Classes More Than 5 Students/Solution.sql +++ b/solution/0500-0599/0596.Classes More Than 5 Students/Solution.sql @@ -1,5 +1,5 @@ -SELECT - class -FROM courses +# Write your MySQL query statement below +SELECT class +FROM Courses GROUP BY class -HAVING COUNT(class) >= 5; +HAVING count(1) >= 5; diff --git a/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README.md b/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README.md index 74b311fe117eb..9bea7b138e21c 100644 --- a/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README.md +++ b/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README.md @@ -102,19 +102,19 @@ RequestAccepted 表: ### **SQL** ```sql +# Write your MySQL query statement below SELECT - IFNULL( - ROUND( + round( + ifnull( ( - SELECT COUNT(DISTINCT requester_id, accepter_id) + SELECT count(DISTINCT requester_id, accepter_id) FROM RequestAccepted ) / ( - SELECT COUNT(DISTINCT sender_id, send_to_id) - FROM FriendRequest + SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest ), - 2 + 0 ), - 0.00 + 2 ) AS accept_rate; ``` diff --git a/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README_EN.md b/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README_EN.md index 2ff608144f4c8..b8c5e4fa681fc 100644 --- a/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README_EN.md +++ b/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/README_EN.md @@ -98,19 +98,19 @@ There are 4 unique accepted requests, and there are 5 requests in total. So the ### **SQL** ```sql +# Write your MySQL query statement below SELECT - IFNULL( - ROUND( + round( + ifnull( ( - SELECT COUNT(DISTINCT requester_id, accepter_id) + SELECT count(DISTINCT requester_id, accepter_id) FROM RequestAccepted ) / ( - SELECT COUNT(DISTINCT sender_id, send_to_id) - FROM FriendRequest + SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest ), - 2 + 0 ), - 0.00 + 2 ) AS accept_rate; ``` diff --git a/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/Solution.sql b/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/Solution.sql index f5cf506b62118..22fad614aef4e 100644 --- a/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/Solution.sql +++ b/solution/0500-0599/0597.Friend Requests I Overall Acceptance Rate/Solution.sql @@ -1,14 +1,14 @@ +# Write your MySQL query statement below SELECT - IFNULL( - ROUND( + round( + ifnull( ( - SELECT COUNT(DISTINCT requester_id, accepter_id) + SELECT count(DISTINCT requester_id, accepter_id) FROM RequestAccepted ) / ( - SELECT COUNT(DISTINCT sender_id, send_to_id) - FROM FriendRequest + SELECT count(DISTINCT sender_id, send_to_id) FROM FriendRequest ), - 2 + 0 ), - 0.00 + 2 ) AS accept_rate;