From dd089f9074898237436ca30c4cd29b044ae84ee5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 11 Jul 2023 22:54:22 +0800 Subject: [PATCH] feat: add sql solutions to lc problems * No.0619.Biggest Single Number * No.0620.Not Boring Movies * No.1045.Customers Who Bought All Products * No.1068.Product Sales Analysis I * No.1069.Product Sales Analysis II * No.1070.Product Sales Analysis III * No.1075.Project Employees I --- .../0619.Biggest Single Number/README.md | 20 +++++++++++++--- .../0619.Biggest Single Number/README_EN.md | 20 +++++++++++++--- .../0619.Biggest Single Number/Solution.sql | 18 ++++++++------- .../0620.Not Boring Movies/README.md | 5 ++-- .../0620.Not Boring Movies/README_EN.md | 5 ++-- .../0620.Not Boring Movies/Solution.sql | 5 ++-- .../0600-0699/0626.Exchange Seats/README.md | 13 +++++++++++ .../0626.Exchange Seats/README_EN.md | 13 +++++++++++ .../README.md | 16 ++++++------- .../README_EN.md | 12 +++------- .../Solution.sql | 12 +++------- .../README.md | 11 +++++---- .../README_EN.md | 7 +++--- .../Solution.sql | 7 +++--- .../1068.Product Sales Analysis I/README.md | 13 ++++++----- .../README_EN.md | 9 +++----- .../Solution.sql | 9 +++----- .../1069.Product Sales Analysis II/README.md | 10 ++++---- .../README_EN.md | 6 ++--- .../Solution.sql | 6 ++--- .../1070.Product Sales Analysis III/README.md | 17 ++++++++++++++ .../README_EN.md | 17 ++++++++++++++ .../Solution.sql | 23 +++++++++---------- .../1075.Project Employees I/README.md | 16 +++++++------ .../1075.Project Employees I/README_EN.md | 12 ++++------ .../1075.Project Employees I/Solution.sql | 12 ++++------ 26 files changed, 191 insertions(+), 123 deletions(-) diff --git a/solution/0600-0699/0619.Biggest Single Number/README.md b/solution/0600-0699/0619.Biggest Single Number/README.md index 39a70b10db5e9..dcbe6549aba70 100644 --- a/solution/0600-0699/0619.Biggest Single Number/README.md +++ b/solution/0600-0699/0619.Biggest Single Number/README.md @@ -96,14 +96,28 @@ MyNumbers table: ### **SQL** ```sql -SELECT MAX(a.num) AS num +# Write your MySQL query statement below +SELECT max(num) AS num FROM ( SELECT num FROM MyNumbers GROUP BY num - HAVING count(*) = 1 - ) AS a; + HAVING count(1) = 1 + ) AS t; +``` + +```sql +# Write your MySQL query statement below +SELECT + CASE + WHEN count(1) = 1 THEN num + ELSE NULL + END AS num +FROM MyNumbers +GROUP BY num +ORDER BY 1 DESC +LIMIT 1; ``` diff --git a/solution/0600-0699/0619.Biggest Single Number/README_EN.md b/solution/0600-0699/0619.Biggest Single Number/README_EN.md index 2fe801f1b22ee..65659dea14f00 100644 --- a/solution/0600-0699/0619.Biggest Single Number/README_EN.md +++ b/solution/0600-0699/0619.Biggest Single Number/README_EN.md @@ -84,14 +84,28 @@ MyNumbers table: ### **SQL** ```sql -SELECT MAX(a.num) AS num +# Write your MySQL query statement below +SELECT max(num) AS num FROM ( SELECT num FROM MyNumbers GROUP BY num - HAVING count(*) = 1 - ) AS a; + HAVING count(1) = 1 + ) AS t; +``` + +```sql +# Write your MySQL query statement below +SELECT + CASE + WHEN count(1) = 1 THEN num + ELSE NULL + END AS num +FROM MyNumbers +GROUP BY num +ORDER BY 1 DESC +LIMIT 1; ``` diff --git a/solution/0600-0699/0619.Biggest Single Number/Solution.sql b/solution/0600-0699/0619.Biggest Single Number/Solution.sql index 98232257e9add..445f06644a945 100644 --- a/solution/0600-0699/0619.Biggest Single Number/Solution.sql +++ b/solution/0600-0699/0619.Biggest Single Number/Solution.sql @@ -1,8 +1,10 @@ -SELECT MAX(a.num) AS num -FROM - ( - SELECT num - FROM MyNumbers - GROUP BY num - HAVING count(*) = 1 - ) AS a; +# Write your MySQL query statement below +SELECT + CASE + WHEN count(1) = 1 THEN num + ELSE NULL + END AS num +FROM MyNumbers +GROUP BY num +ORDER BY 1 DESC +LIMIT 1; diff --git a/solution/0600-0699/0620.Not Boring Movies/README.md b/solution/0600-0699/0620.Not Boring Movies/README.md index 5284d3a43c487..f6994c36636c1 100644 --- a/solution/0600-0699/0620.Not Boring Movies/README.md +++ b/solution/0600-0699/0620.Not Boring Movies/README.md @@ -48,9 +48,10 @@ ### **SQL** ```sql +# Write your MySQL query statement below SELECT * -FROM cinema -WHERE description NOT LIKE '%boring%' AND mod(id, 2) = 1 +FROM Cinema +WHERE description != 'boring' AND id % 2 = 1 ORDER BY rating DESC; ``` diff --git a/solution/0600-0699/0620.Not Boring Movies/README_EN.md b/solution/0600-0699/0620.Not Boring Movies/README_EN.md index 143a1b78a4b76..c833c6f30a87b 100644 --- a/solution/0600-0699/0620.Not Boring Movies/README_EN.md +++ b/solution/0600-0699/0620.Not Boring Movies/README_EN.md @@ -61,9 +61,10 @@ We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 i ### **SQL** ```sql +# Write your MySQL query statement below SELECT * -FROM cinema -WHERE description NOT LIKE '%boring%' AND mod(id, 2) = 1 +FROM Cinema +WHERE description != 'boring' AND id % 2 = 1 ORDER BY rating DESC; ``` diff --git a/solution/0600-0699/0620.Not Boring Movies/Solution.sql b/solution/0600-0699/0620.Not Boring Movies/Solution.sql index 062d6a3f23e4e..c77866e636b26 100644 --- a/solution/0600-0699/0620.Not Boring Movies/Solution.sql +++ b/solution/0600-0699/0620.Not Boring Movies/Solution.sql @@ -1,4 +1,5 @@ +# Write your MySQL query statement below SELECT * -FROM cinema -WHERE description NOT LIKE '%boring%' AND mod(id, 2) = 1 +FROM Cinema +WHERE description != 'boring' AND id % 2 = 1 ORDER BY rating DESC; diff --git a/solution/0600-0699/0626.Exchange Seats/README.md b/solution/0600-0699/0626.Exchange Seats/README.md index 35593f7e1167d..bd0f42d8bb5d5 100644 --- a/solution/0600-0699/0626.Exchange Seats/README.md +++ b/solution/0600-0699/0626.Exchange Seats/README.md @@ -103,4 +103,17 @@ from seat ``` +```sql +# Write your MySQL query statement below +SELECT + CASE + WHEN id & 1 = 0 THEN id - 1 + WHEN row_number() OVER (ORDER BY id) != count(id) OVER () THEN id + 1 + ELSE id + END AS id, + student +FROM Seat +ORDER BY 1; +``` + diff --git a/solution/0600-0699/0626.Exchange Seats/README_EN.md b/solution/0600-0699/0626.Exchange Seats/README_EN.md index 07a86202304cd..3fb069c262c15 100644 --- a/solution/0600-0699/0626.Exchange Seats/README_EN.md +++ b/solution/0600-0699/0626.Exchange Seats/README_EN.md @@ -99,4 +99,17 @@ from seat ``` +```sql +# Write your MySQL query statement below +SELECT + CASE + WHEN id & 1 = 0 THEN id - 1 + WHEN row_number() OVER (ORDER BY id) != count(id) OVER () THEN id + 1 + ELSE id + END AS id, + student +FROM Seat +ORDER BY 1; +``` + diff --git a/solution/1000-1099/1045.Customers Who Bought All Products/README.md b/solution/1000-1099/1045.Customers Who Bought All Products/README.md index c501884d4ff5c..4bf0c3e18b31a 100644 --- a/solution/1000-1099/1045.Customers Who Bought All Products/README.md +++ b/solution/1000-1099/1045.Customers Who Bought All Products/README.md @@ -66,22 +66,20 @@ Result 表: +**方法一:GROUP BY + HAVING** + +我们将 `Customer` 表按照 `customer_id` 进行分组,然后使用 `HAVING` 子句筛选出购买了所有产品的客户。 + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - customer_id +SELECT customer_id FROM Customer -GROUP BY customer_id -HAVING - COUNT(DISTINCT product_key) = ( - SELECT - COUNT(1) - FROM Product - ); +GROUP BY 1 +HAVING count(DISTINCT product_key) = (SELECT count(1) FROM Product); ``` diff --git a/solution/1000-1099/1045.Customers Who Bought All Products/README_EN.md b/solution/1000-1099/1045.Customers Who Bought All Products/README_EN.md index 073077b05238c..1d6286b8badc3 100644 --- a/solution/1000-1099/1045.Customers Who Bought All Products/README_EN.md +++ b/solution/1000-1099/1045.Customers Who Bought All Products/README_EN.md @@ -79,16 +79,10 @@ The customers who bought all the products (5 and 6) are customers with IDs 1 and ```sql # Write your MySQL query statement below -SELECT - customer_id +SELECT customer_id FROM Customer -GROUP BY customer_id -HAVING - COUNT(DISTINCT product_key) = ( - SELECT - COUNT(1) - FROM Product - ); +GROUP BY 1 +HAVING count(DISTINCT product_key) = (SELECT count(1) FROM Product); ``` diff --git a/solution/1000-1099/1045.Customers Who Bought All Products/Solution.sql b/solution/1000-1099/1045.Customers Who Bought All Products/Solution.sql index 0017dcff21192..6ec5c9985d884 100644 --- a/solution/1000-1099/1045.Customers Who Bought All Products/Solution.sql +++ b/solution/1000-1099/1045.Customers Who Bought All Products/Solution.sql @@ -1,11 +1,5 @@ # Write your MySQL query statement below -SELECT - customer_id +SELECT customer_id FROM Customer -GROUP BY customer_id -HAVING - COUNT(DISTINCT product_key) = ( - SELECT - COUNT(1) - FROM Product - ); +GROUP BY 1 +HAVING count(DISTINCT product_key) = (SELECT count(1) FROM Product); diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md index f6a988e2de12f..13fe6f6c300fe 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md @@ -51,18 +51,19 @@ Result 表: -`GROUP BY` + `HAVING` 解决。 +**方法一:使用 `GROUP BY` + `HAVING`** + +我们将 `ActorDirector` 表按照 `actor_id` 和 `director_id` 进行分组,然后使用 `HAVING` 过滤出合作次数大于等于 $3$ 次的组。 ### **SQL** ```sql -SELECT - actor_id, - director_id +# Write your MySQL query statement below +SELECT actor_id, director_id FROM ActorDirector -GROUP BY actor_id, director_id +GROUP BY 1, 2 HAVING count(1) >= 3; ``` diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md index 22d36627b7ef8..f720c682c3a8f 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md @@ -60,11 +60,10 @@ Use `GROUP BY` & `HAVING`. ### **SQL** ```sql -SELECT - actor_id, - director_id +# Write your MySQL query statement below +SELECT actor_id, director_id FROM ActorDirector -GROUP BY actor_id, director_id +GROUP BY 1, 2 HAVING count(1) >= 3; ``` diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/Solution.sql b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/Solution.sql index 963f7bf637fef..37c825437b465 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/Solution.sql +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/Solution.sql @@ -1,6 +1,5 @@ -SELECT - actor_id, - director_id +# Write your MySQL query statement below +SELECT actor_id, director_id FROM ActorDirector -GROUP BY actor_id, director_id +GROUP BY 1, 2 HAVING count(1) >= 3; diff --git a/solution/1000-1099/1068.Product Sales Analysis I/README.md b/solution/1000-1099/1068.Product Sales Analysis I/README.md index 3c8a3219f3ae5..f39590050231e 100644 --- a/solution/1000-1099/1068.Product Sales Analysis I/README.md +++ b/solution/1000-1099/1068.Product Sales Analysis I/README.md @@ -78,19 +78,20 @@ Result 表: +**方法一:使用 `JOIN` 内连接** + +我们直接使用 `JOIN` 连接 `Sales` 和 `Product` 两张表,连接字段为 `product_id`,然后选择需要的字段即可。 + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - p.product_name AS product_name, - s.year AS year, - s.price AS price +SELECT product_name, year, price FROM - Sales AS s - LEFT JOIN Product AS p ON s.product_id = p.product_id; + Sales + JOIN Product USING (product_id); ``` diff --git a/solution/1000-1099/1068.Product Sales Analysis I/README_EN.md b/solution/1000-1099/1068.Product Sales Analysis I/README_EN.md index f4d62c52f7de6..c742af1b2eb72 100644 --- a/solution/1000-1099/1068.Product Sales Analysis I/README_EN.md +++ b/solution/1000-1099/1068.Product Sales Analysis I/README_EN.md @@ -88,13 +88,10 @@ From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011. ```sql # Write your MySQL query statement below -SELECT - p.product_name AS product_name, - s.year AS year, - s.price AS price +SELECT product_name, year, price FROM - Sales AS s - LEFT JOIN Product AS p ON s.product_id = p.product_id; + Sales + JOIN Product USING (product_id); ``` diff --git a/solution/1000-1099/1068.Product Sales Analysis I/Solution.sql b/solution/1000-1099/1068.Product Sales Analysis I/Solution.sql index 0ddd974e1ae47..20f74faba30e4 100644 --- a/solution/1000-1099/1068.Product Sales Analysis I/Solution.sql +++ b/solution/1000-1099/1068.Product Sales Analysis I/Solution.sql @@ -1,8 +1,5 @@ # Write your MySQL query statement below -SELECT - p.product_name AS product_name, - s.year AS year, - s.price AS price +SELECT product_name, year, price FROM - Sales AS s - LEFT JOIN Product AS p ON s.product_id = p.product_id; + Sales + JOIN Product USING (product_id); diff --git a/solution/1000-1099/1069.Product Sales Analysis II/README.md b/solution/1000-1099/1069.Product Sales Analysis II/README.md index fc085318b2a1c..603361d8ef651 100644 --- a/solution/1000-1099/1069.Product Sales Analysis II/README.md +++ b/solution/1000-1099/1069.Product Sales Analysis II/README.md @@ -71,17 +71,19 @@ Result 表: +**方法一:使用 `GROUP BY`** + +我们可以使用 `GROUP BY`,按照 `product_id` 分组,然后每一组对 `quantity` 求和。 + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - product_id, - sum(quantity) AS total_quantity +SELECT product_id, sum(quantity) AS total_quantity FROM Sales -GROUP BY product_id; +GROUP BY 1; ``` diff --git a/solution/1000-1099/1069.Product Sales Analysis II/README_EN.md b/solution/1000-1099/1069.Product Sales Analysis II/README_EN.md index 1638d3e790d0f..b4efffa249023 100644 --- a/solution/1000-1099/1069.Product Sales Analysis II/README_EN.md +++ b/solution/1000-1099/1069.Product Sales Analysis II/README_EN.md @@ -83,11 +83,9 @@ Product table: ```sql # Write your MySQL query statement below -SELECT - product_id, - sum(quantity) AS total_quantity +SELECT product_id, sum(quantity) AS total_quantity FROM Sales -GROUP BY product_id; +GROUP BY 1; ``` diff --git a/solution/1000-1099/1069.Product Sales Analysis II/Solution.sql b/solution/1000-1099/1069.Product Sales Analysis II/Solution.sql index 4712e1cd78cab..509e32bf47165 100644 --- a/solution/1000-1099/1069.Product Sales Analysis II/Solution.sql +++ b/solution/1000-1099/1069.Product Sales Analysis II/Solution.sql @@ -1,6 +1,4 @@ # Write your MySQL query statement below -SELECT - product_id, - sum(quantity) AS total_quantity +SELECT product_id, sum(quantity) AS total_quantity FROM Sales -GROUP BY product_id; +GROUP BY 1; diff --git a/solution/1000-1099/1070.Product Sales Analysis III/README.md b/solution/1000-1099/1070.Product Sales Analysis III/README.md index 2345307857865..540c945e3d265 100644 --- a/solution/1000-1099/1070.Product Sales Analysis III/README.md +++ b/solution/1000-1099/1070.Product Sales Analysis III/README.md @@ -102,4 +102,21 @@ WHERE ); ``` +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + rank() OVER ( + PARTITION BY product_id + ORDER BY year + ) AS rk + FROM Sales + ) +SELECT product_id, year AS first_year, quantity, price +FROM T +WHERE rk = 1; +``` + diff --git a/solution/1000-1099/1070.Product Sales Analysis III/README_EN.md b/solution/1000-1099/1070.Product Sales Analysis III/README_EN.md index 8dfba5218ee9b..db894f6dda601 100644 --- a/solution/1000-1099/1070.Product Sales Analysis III/README_EN.md +++ b/solution/1000-1099/1070.Product Sales Analysis III/README_EN.md @@ -99,4 +99,21 @@ WHERE ); ``` +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + rank() OVER ( + PARTITION BY product_id + ORDER BY year + ) AS rk + FROM Sales + ) +SELECT product_id, year AS first_year, quantity, price +FROM T +WHERE rk = 1; +``` + diff --git a/solution/1000-1099/1070.Product Sales Analysis III/Solution.sql b/solution/1000-1099/1070.Product Sales Analysis III/Solution.sql index 0e817c23296b6..e2d64dadff69b 100644 --- a/solution/1000-1099/1070.Product Sales Analysis III/Solution.sql +++ b/solution/1000-1099/1070.Product Sales Analysis III/Solution.sql @@ -1,15 +1,14 @@ # Write your MySQL query statement below -SELECT - product_id, - year AS first_year, - quantity, - price -FROM Sales -WHERE - (product_id, year) IN ( +WITH + T AS ( SELECT - product_id, - min(year) AS year + *, + rank() OVER ( + PARTITION BY product_id + ORDER BY year + ) AS rk FROM Sales - GROUP BY product_id - ); + ) +SELECT product_id, year AS first_year, quantity, price +FROM T +WHERE rk = 1; diff --git a/solution/1000-1099/1075.Project Employees I/README.md b/solution/1000-1099/1075.Project Employees I/README.md index 8f72a75129a4e..2fae001bb67cc 100644 --- a/solution/1000-1099/1075.Project Employees I/README.md +++ b/solution/1000-1099/1075.Project Employees I/README.md @@ -74,19 +74,21 @@ Result 表: +**方法一:内连接 + `GROUP BY` 分组** + +我们可以通过内连接将两张表连接起来,然后通过 `GROUP BY` 分组,最后使用 `AVG` 函数求工作年限的平均值。 + ### **SQL** ```sql -# Write your MySQL query statement below -SELECT - project_id, - round(AVG(experience_years), 2) AS average_years +# Write your MySQL query statement +SELECT project_id, round(avg(experience_years), 2) AS average_years FROM - Project AS p - LEFT JOIN Employee AS e ON p.employee_id = e.employee_id -GROUP BY project_id; + Project + JOIN Employee USING (employee_id) +GROUP BY 1; ``` diff --git a/solution/1000-1099/1075.Project Employees I/README_EN.md b/solution/1000-1099/1075.Project Employees I/README_EN.md index be0c898edf649..bc699f46fc5d5 100644 --- a/solution/1000-1099/1075.Project Employees I/README_EN.md +++ b/solution/1000-1099/1075.Project Employees I/README_EN.md @@ -83,14 +83,12 @@ Employee table: ### **SQL** ```sql -# Write your MySQL query statement below -SELECT - project_id, - round(AVG(experience_years), 2) AS average_years +# Write your MySQL query statement +SELECT project_id, round(avg(experience_years), 2) AS average_years FROM - Project AS p - LEFT JOIN Employee AS e ON p.employee_id = e.employee_id -GROUP BY project_id; + Project + JOIN Employee USING (employee_id) +GROUP BY 1; ``` diff --git a/solution/1000-1099/1075.Project Employees I/Solution.sql b/solution/1000-1099/1075.Project Employees I/Solution.sql index aa7a81a20ff02..10bdbc392bbde 100644 --- a/solution/1000-1099/1075.Project Employees I/Solution.sql +++ b/solution/1000-1099/1075.Project Employees I/Solution.sql @@ -1,8 +1,6 @@ -# Write your MySQL query statement below -SELECT - project_id, - round(AVG(experience_years), 2) AS average_years +# Write your MySQL query statement +SELECT project_id, round(avg(experience_years), 2) AS average_years FROM - Project AS p - LEFT JOIN Employee AS e ON p.employee_id = e.employee_id -GROUP BY project_id; + Project + JOIN Employee USING (employee_id) +GROUP BY 1;