diff --git a/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README.md b/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README.md index dde25be8327b6..2624c26c22677 100644 --- a/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README.md +++ b/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README.md @@ -69,7 +69,29 @@ Output table is ordered by peak_calling_hour and city in descending order. ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY city + ORDER BY cnt DESC + ) AS rk + FROM + ( + SELECT + city, + HOUR(call_time) AS h, + COUNT(1) AS cnt + FROM Calls + GROUP BY 1, 2 + ) AS t + ) +SELECT city, h AS peak_calling_hour, cnt AS number_of_calls +FROM T +WHERE rk = 1 +ORDER BY 2 DESC, 1 DESC; ``` diff --git a/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README_EN.md b/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README_EN.md index f9f7e2ce28e7b..b402b3a54e5ff 100644 --- a/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README_EN.md +++ b/solution/2900-2999/2984.Find Peak Calling Hours for Each City/README_EN.md @@ -63,7 +63,29 @@ Output table is ordered by peak_calling_hour and city in descending order. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY city + ORDER BY cnt DESC + ) AS rk + FROM + ( + SELECT + city, + HOUR(call_time) AS h, + COUNT(1) AS cnt + FROM Calls + GROUP BY 1, 2 + ) AS t + ) +SELECT city, h AS peak_calling_hour, cnt AS number_of_calls +FROM T +WHERE rk = 1 +ORDER BY 2 DESC, 1 DESC; ``` diff --git a/solution/2900-2999/2984.Find Peak Calling Hours for Each City/Solution.sql b/solution/2900-2999/2984.Find Peak Calling Hours for Each City/Solution.sql new file mode 100644 index 0000000000000..e0f32257c9e2a --- /dev/null +++ b/solution/2900-2999/2984.Find Peak Calling Hours for Each City/Solution.sql @@ -0,0 +1,23 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY city + ORDER BY cnt DESC + ) AS rk + FROM + ( + SELECT + city, + HOUR(call_time) AS h, + COUNT(1) AS cnt + FROM Calls + GROUP BY 1, 2 + ) AS t + ) +SELECT city, h AS peak_calling_hour, cnt AS number_of_calls +FROM T +WHERE rk = 1 +ORDER BY 2 DESC, 1 DESC; diff --git a/solution/2900-2999/2985.Calculate Compressed Mean/README.md b/solution/2900-2999/2985.Calculate Compressed Mean/README.md index 600e3a7372e10..db35d9c8a6333 100644 --- a/solution/2900-2999/2985.Calculate Compressed Mean/README.md +++ b/solution/2900-2999/2985.Calculate Compressed Mean/README.md @@ -63,7 +63,13 @@ The calculation is as follows: ```sql - +# Write your MySQL query statement below +SELECT + ROUND( + SUM(item_count * order_occurrences) / SUM(order_occurrences), + 2 + ) AS average_items_per_order +FROM Orders; ``` diff --git a/solution/2900-2999/2985.Calculate Compressed Mean/README_EN.md b/solution/2900-2999/2985.Calculate Compressed Mean/README_EN.md index 526f2ddb0df55..bcd79bd720e27 100644 --- a/solution/2900-2999/2985.Calculate Compressed Mean/README_EN.md +++ b/solution/2900-2999/2985.Calculate Compressed Mean/README_EN.md @@ -57,7 +57,13 @@ The calculation is as follows: ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT + ROUND( + SUM(item_count * order_occurrences) / SUM(order_occurrences), + 2 + ) AS average_items_per_order +FROM Orders; ``` diff --git a/solution/2900-2999/2985.Calculate Compressed Mean/Solution.sql b/solution/2900-2999/2985.Calculate Compressed Mean/Solution.sql new file mode 100644 index 0000000000000..6cb645b0dfacd --- /dev/null +++ b/solution/2900-2999/2985.Calculate Compressed Mean/Solution.sql @@ -0,0 +1,7 @@ +# Write your MySQL query statement below +SELECT + ROUND( + SUM(item_count * order_occurrences) / SUM(order_occurrences), + 2 + ) AS average_items_per_order +FROM Orders; diff --git a/solution/2900-2999/2986.Find Third Transaction/README.md b/solution/2900-2999/2986.Find Third Transaction/README.md index 041e857f747bd..d60ca000b0cc6 100644 --- a/solution/2900-2999/2986.Find Third Transaction/README.md +++ b/solution/2900-2999/2986.Find Third Transaction/README.md @@ -70,7 +70,32 @@ Output table is ordered by user_id in ascending order. ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) AS rk, + spend > ( + LAG(spend) OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) + ) + AND spend > ( + LAG(spend, 2) OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) + ) AS st + FROM Transactions + ) +SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date +FROM T +WHERE rk = 3 AND st = 1; ``` diff --git a/solution/2900-2999/2986.Find Third Transaction/README_EN.md b/solution/2900-2999/2986.Find Third Transaction/README_EN.md index fd212515259d4..85d3d6a6a8b29 100644 --- a/solution/2900-2999/2986.Find Third Transaction/README_EN.md +++ b/solution/2900-2999/2986.Find Third Transaction/README_EN.md @@ -64,7 +64,32 @@ Output table is ordered by user_id in ascending order. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) AS rk, + spend > ( + LAG(spend) OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) + ) + AND spend > ( + LAG(spend, 2) OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) + ) AS st + FROM Transactions + ) +SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date +FROM T +WHERE rk = 3 AND st = 1; ``` diff --git a/solution/2900-2999/2986.Find Third Transaction/Solution.sql b/solution/2900-2999/2986.Find Third Transaction/Solution.sql new file mode 100644 index 0000000000000..b9e821b48efae --- /dev/null +++ b/solution/2900-2999/2986.Find Third Transaction/Solution.sql @@ -0,0 +1,26 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) AS rk, + spend > ( + LAG(spend) OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) + ) + AND spend > ( + LAG(spend, 2) OVER ( + PARTITION BY user_id + ORDER BY transaction_date + ) + ) AS st + FROM Transactions + ) +SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date +FROM T +WHERE rk = 3 AND st = 1; diff --git a/solution/2900-2999/2987.Find Expensive Cities/README.md b/solution/2900-2999/2987.Find Expensive Cities/README.md index ee0892aab62d5..830e08e2f7daa 100644 --- a/solution/2900-2999/2987.Find Expensive Cities/README.md +++ b/solution/2900-2999/2987.Find Expensive Cities/README.md @@ -75,7 +75,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave ```sql - +# Write your MySQL query statement below +SELECT city +FROM Listings +GROUP BY city +HAVING AVG(price) > (SELECT AVG(price) FROM Listings) +ORDER BY 1; ``` diff --git a/solution/2900-2999/2987.Find Expensive Cities/README_EN.md b/solution/2900-2999/2987.Find Expensive Cities/README_EN.md index 82edab24558aa..3a156e76b097c 100644 --- a/solution/2900-2999/2987.Find Expensive Cities/README_EN.md +++ b/solution/2900-2999/2987.Find Expensive Cities/README_EN.md @@ -69,7 +69,12 @@ Only Chicago and Los Angeles have average home prices exceeding the national ave ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT city +FROM Listings +GROUP BY city +HAVING AVG(price) > (SELECT AVG(price) FROM Listings) +ORDER BY 1; ``` diff --git a/solution/2900-2999/2987.Find Expensive Cities/Solution.sql b/solution/2900-2999/2987.Find Expensive Cities/Solution.sql new file mode 100644 index 0000000000000..274e8458dc51b --- /dev/null +++ b/solution/2900-2999/2987.Find Expensive Cities/Solution.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below +SELECT city +FROM Listings +GROUP BY city +HAVING AVG(price) > (SELECT AVG(price) FROM Listings) +ORDER BY 1; diff --git a/solution/2900-2999/2988.Manager of the Largest Department/README.md b/solution/2900-2999/2988.Manager of the Largest Department/README.md index 0398f8f72bd83..a33764cba0c50 100644 --- a/solution/2900-2999/2988.Manager of the Largest Department/README.md +++ b/solution/2900-2999/2988.Manager of the Largest Department/README.md @@ -72,7 +72,19 @@ Output table is ordered by dep_id in ascending order. ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT dep_id, COUNT(1) AS cnt + FROM Employees + GROUP BY 1 + ) +SELECT emp_name AS manager_name, t.dep_id +FROM + T AS t + JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager' +WHERE cnt = (SELECT MAX(cnt) FROM T) +ORDER BY 2; ``` diff --git a/solution/2900-2999/2988.Manager of the Largest Department/README_EN.md b/solution/2900-2999/2988.Manager of the Largest Department/README_EN.md index 39495291a1bdd..2b8e5cf25b915 100644 --- a/solution/2900-2999/2988.Manager of the Largest Department/README_EN.md +++ b/solution/2900-2999/2988.Manager of the Largest Department/README_EN.md @@ -66,7 +66,19 @@ Output table is ordered by dep_id in ascending order. ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT dep_id, COUNT(1) AS cnt + FROM Employees + GROUP BY 1 + ) +SELECT emp_name AS manager_name, t.dep_id +FROM + T AS t + JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager' +WHERE cnt = (SELECT MAX(cnt) FROM T) +ORDER BY 2; ``` diff --git a/solution/2900-2999/2988.Manager of the Largest Department/Solution.sql b/solution/2900-2999/2988.Manager of the Largest Department/Solution.sql new file mode 100644 index 0000000000000..adc52180dd285 --- /dev/null +++ b/solution/2900-2999/2988.Manager of the Largest Department/Solution.sql @@ -0,0 +1,13 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT dep_id, COUNT(1) AS cnt + FROM Employees + GROUP BY 1 + ) +SELECT emp_name AS manager_name, t.dep_id +FROM + T AS t + JOIN Employees AS e ON t.dep_id = e.dep_id AND e.position = 'Manager' +WHERE cnt = (SELECT MAX(cnt) FROM T) +ORDER BY 2; diff --git a/solution/2900-2999/2989.Class Performance/README.md b/solution/2900-2999/2989.Class Performance/README.md index 48185755b10b4..08d586d23adc3 100644 --- a/solution/2900-2999/2989.Class Performance/README.md +++ b/solution/2900-2999/2989.Class Performance/README.md @@ -64,6 +64,10 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest +**方法一:最大值最小值** + +我们可以使用 `MAX` 和 `MIN` 函数来分别获取 `assignment1`、`assignment2`、`assignment3` 的和的最大值和最小值,然后相减即可。 + ### **SQL** @@ -71,7 +75,12 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest ```sql - +# Write your MySQL query statement below +SELECT + MAX(assignment1 + assignment2 + assignment3) - MIN( + assignment1 + assignment2 + assignment3 + ) AS difference_in_score +FROM Scores; ``` diff --git a/solution/2900-2999/2989.Class Performance/README_EN.md b/solution/2900-2999/2989.Class Performance/README_EN.md index 8bf16802f3592..1f0233186bb80 100644 --- a/solution/2900-2999/2989.Class Performance/README_EN.md +++ b/solution/2900-2999/2989.Class Performance/README_EN.md @@ -60,12 +60,21 @@ student_id 321 has the highest score of 230, while student_id 896 has the lowest ## Solutions +**Solution 1: Maximum and Minimum** + +We can use the `MAX` and `MIN` functions to get the maximum and minimum sums of `assignment1`, `assignment2`, and `assignment3`, respectively. Then, subtract the minimum from the maximum. + ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT + MAX(assignment1 + assignment2 + assignment3) - MIN( + assignment1 + assignment2 + assignment3 + ) AS difference_in_score +FROM Scores; ``` diff --git a/solution/2900-2999/2989.Class Performance/Solution.sql b/solution/2900-2999/2989.Class Performance/Solution.sql new file mode 100644 index 0000000000000..4bf6b6d909259 --- /dev/null +++ b/solution/2900-2999/2989.Class Performance/Solution.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below +SELECT + MAX(assignment1 + assignment2 + assignment3) - MIN( + assignment1 + assignment2 + assignment3 + ) AS difference_in_score +FROM Scores; diff --git a/solution/2900-2999/2990.Loan Types/README.md b/solution/2900-2999/2990.Loan Types/README.md index e74f1613e767f..6e1a3eb18a5ec 100644 --- a/solution/2900-2999/2990.Loan Types/README.md +++ b/solution/2900-2999/2990.Loan Types/README.md @@ -62,6 +62,10 @@ Output table is ordered by user_id in ascending order. +**方法一:分组求和** + +我们可以对 `Loans` 表按照 `user_id` 进行分组,找出既包含 `Refinance` 又包含 `Mortgage` 的用户,然后按照 `user_id` 进行排序。 + ### **SQL** @@ -69,7 +73,12 @@ Output table is ordered by user_id in ascending order. ```sql - +# Write your MySQL query statement below +SELECT user_id +FROM Loans +GROUP BY 1 +HAVING SUM(loan_type = 'Refinance') > 0 AND SUM(loan_type = 'Mortgage') > 0 +ORDER BY 1; ``` diff --git a/solution/2900-2999/2990.Loan Types/README_EN.md b/solution/2900-2999/2990.Loan Types/README_EN.md index 5b16e6c3c8e17..bd004d0542d3c 100644 --- a/solution/2900-2999/2990.Loan Types/README_EN.md +++ b/solution/2900-2999/2990.Loan Types/README_EN.md @@ -58,12 +58,21 @@ Output table is ordered by user_id in ascending order. ## Solutions +**Solution 1: Grouping and Summation** + +We can group the `Loans` table by `user_id` to find users who have both `Refinance` and `Mortgage`. Then, sort the results by `user_id`. + ### **SQL** ```sql - +# Write your MySQL query statement below +SELECT user_id +FROM Loans +GROUP BY 1 +HAVING SUM(loan_type = 'Refinance') > 0 AND SUM(loan_type = 'Mortgage') > 0 +ORDER BY 1; ``` diff --git a/solution/2900-2999/2990.Loan Types/Solution.sql b/solution/2900-2999/2990.Loan Types/Solution.sql new file mode 100644 index 0000000000000..99961d4649aed --- /dev/null +++ b/solution/2900-2999/2990.Loan Types/Solution.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below +SELECT user_id +FROM Loans +GROUP BY 1 +HAVING SUM(loan_type = 'Refinance') > 0 AND SUM(loan_type = 'Mortgage') > 0 +ORDER BY 1; diff --git a/solution/2900-2999/2991.Top Three Wineries/README.md b/solution/2900-2999/2991.Top Three Wineries/README.md index 459fd130225f5..61f662623c882 100644 --- a/solution/2900-2999/2991.Top Three Wineries/README.md +++ b/solution/2900-2999/2991.Top Three Wineries/README.md @@ -75,6 +75,23 @@ Output table is ordered by country in ascending order. +**方法一:分组 + 窗口函数 + 左连接** + +我们可以先对 `Wineries` 表按照 `country` 和 `winery` 进行分组,计算每个分组的总得分 `points`,然后再利用窗口函数 `RANK()` 将数据再按照 `country` 进行分组,按照 `points` 降序、`winery` 升序进行排序,并且用 `CONCAT()` 函数将 `winery` 和 `points` 进行拼接,得到如下形式的数据,记为 `T` 表: + +| country | winery | rk | +| --------- | -------------------- | --- | +| Australia | HarmonyHill (100) | 1 | +| Australia | GrapesGalore (85) | 2 | +| Australia | WhisperingPines (84) | 3 | +| Hungary | MoonlitCellars (60) | 1 | +| India | SunsetVines (69) | 1 | +| USA | RoyalVines (86) | 1 | +| USA | Eagle'sNest (45) | 2 | +| USA | PacificCrest (9) | 3 | + +接下来,我们只需要筛选出 `rk = 1` 的数据,然后再将 `T` 表自连接两次,分别连接 `rk = 2` 和 `rk = 3` 的数据,即可得到最终结果。 + ### **SQL** @@ -82,7 +99,29 @@ Output table is ordered by country in ascending order. ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + country, + CONCAT(winery, ' (', points, ')') AS winery, + RANK() OVER ( + PARTITION BY country + ORDER BY points DESC, winery + ) AS rk + FROM (SELECT country, SUM(points) AS points, winery FROM Wineries GROUP BY 1, 3) AS t + ) +SELECT + t1.country, + t1.winery AS top_winery, + IFNULL(t2.winery, 'No second winery') AS second_winery, + IFNULL(t3.winery, 'No third winery') AS third_winery +FROM + T AS t1 + LEFT JOIN T AS t2 ON t1.country = t2.country AND t1.rk = t2.rk - 1 + LEFT JOIN T AS t3 ON t2.country = t3.country AND t2.rk = t3.rk - 1 +WHERE t1.rk = 1 +ORDER BY 1; ``` diff --git a/solution/2900-2999/2991.Top Three Wineries/README_EN.md b/solution/2900-2999/2991.Top Three Wineries/README_EN.md index a2518fcd30793..da2d74abda4ff 100644 --- a/solution/2900-2999/2991.Top Three Wineries/README_EN.md +++ b/solution/2900-2999/2991.Top Three Wineries/README_EN.md @@ -71,12 +71,51 @@ Output table is ordered by country in ascending order. ## Solutions +**Solution 1: Grouping + Window Function + Left Join** + +We can first group the `Wineries` table by `country` and `winery`, calculate the total score `points` for each group, then use the window function `RANK()` to group the data by `country` again, sort by `points` in descending order and `winery` in ascending order, and use the `CONCAT()` function to concatenate `winery` and `points`, resulting in the following data, denoted as table `T`: + +| country | winery | rk | +| --------- | -------------------- | --- | +| Australia | HarmonyHill (100) | 1 | +| Australia | GrapesGalore (85) | 2 | +| Australia | WhisperingPines (84) | 3 | +| Hungary | MoonlitCellars (60) | 1 | +| India | SunsetVines (69) | 1 | +| USA | RoyalVines (86) | 1 | +| USA | Eagle'sNest (45) | 2 | +| USA | PacificCrest (9) | 3 | + +Next, we just need to filter out the data where `rk = 1`, then join table `T` to itself twice, connecting the data where `rk = 2` and `rk = 3` respectively, to get the final result. + ### **SQL** ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + country, + CONCAT(winery, ' (', points, ')') AS winery, + RANK() OVER ( + PARTITION BY country + ORDER BY points DESC, winery + ) AS rk + FROM (SELECT country, SUM(points) AS points, winery FROM Wineries GROUP BY 1, 3) AS t + ) +SELECT + t1.country, + t1.winery AS top_winery, + IFNULL(t2.winery, 'No second winery') AS second_winery, + IFNULL(t3.winery, 'No third winery') AS third_winery +FROM + T AS t1 + LEFT JOIN T AS t2 ON t1.country = t2.country AND t1.rk = t2.rk - 1 + LEFT JOIN T AS t3 ON t2.country = t3.country AND t2.rk = t3.rk - 1 +WHERE t1.rk = 1 +ORDER BY 1; ``` diff --git a/solution/2900-2999/2991.Top Three Wineries/Solution.sql b/solution/2900-2999/2991.Top Three Wineries/Solution.sql new file mode 100644 index 0000000000000..ff4322e4d434b --- /dev/null +++ b/solution/2900-2999/2991.Top Three Wineries/Solution.sql @@ -0,0 +1,23 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT + country, + CONCAT(winery, ' (', points, ')') AS winery, + RANK() OVER ( + PARTITION BY country + ORDER BY points DESC, winery + ) AS rk + FROM (SELECT country, SUM(points) AS points, winery FROM Wineries GROUP BY 1, 3) AS t + ) +SELECT + t1.country, + t1.winery AS top_winery, + IFNULL(t2.winery, 'No second winery') AS second_winery, + IFNULL(t3.winery, 'No third winery') AS third_winery +FROM + T AS t1 + LEFT JOIN T AS t2 ON t1.country = t2.country AND t1.rk = t2.rk - 1 + LEFT JOIN T AS t3 ON t2.country = t3.country AND t2.rk = t3.rk - 1 +WHERE t1.rk = 1 +ORDER BY 1;