From 96d30718f9ec24dc3460b746b93e2929a680308c Mon Sep 17 00:00:00 2001 From: Frisk <79501315+Frisk0316@users.noreply.github.com> Date: Fri, 5 May 2023 20:22:55 +0800 Subject: [PATCH 1/3] Create 1378. Replace Employee ID With The Unique Identifier # finished at 2023/05/05 # Runtime beats 15.78% --- ...ace Employee ID With The Unique Identifier | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 1378. Replace Employee ID With The Unique Identifier diff --git a/1378. Replace Employee ID With The Unique Identifier b/1378. Replace Employee ID With The Unique Identifier new file mode 100644 index 0000000..bb47b38 --- /dev/null +++ b/1378. Replace Employee ID With The Unique Identifier @@ -0,0 +1,79 @@ +/* +Problem Descirption: ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| id | int | +| name | varchar | ++---------------+---------+ +id is the primary key for this table. +Each row of this table contains the id and the name of an employee in a company. + + + +Table: EmployeeUNI + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| id | int | +| unique_id | int | ++---------------+---------+ +(id, unique_id) is the primary key for this table. +Each row of this table contains the id and the corresponding unique id of an employee in the company. + + + +Write an SQL query to show the unique ID of each user, If a user does not have a unique ID replace just show null. + +Return the result table in any order. + +The query result format is in the following example. + + + +Example 1: + +Input: +Employees table: ++----+----------+ +| id | name | ++----+----------+ +| 1 | Alice | +| 7 | Bob | +| 11 | Meir | +| 90 | Winston | +| 3 | Jonathan | ++----+----------+ +EmployeeUNI table: ++----+-----------+ +| id | unique_id | ++----+-----------+ +| 3 | 1 | +| 11 | 2 | +| 90 | 3 | ++----+-----------+ +Output: ++-----------+----------+ +| unique_id | name | ++-----------+----------+ +| null | Alice | +| null | Bob | +| 2 | Meir | +| 3 | Winston | +| 1 | Jonathan | ++-----------+----------+ +Explanation: +Alice and Bob do not have a unique ID, We will show null instead. +The unique ID of Meir is 2. +The unique ID of Winston is 3. +The unique ID of Jonathan is 1. + +*/ + +# Write your MySQL query statement below + +SELECT unique_id, name +FROM Employees +LEFT JOIN EmployeeUNI +USING (id) From 2aea29ec123cbbe5f05820259f514a64197d03b4 Mon Sep 17 00:00:00 2001 From: Frisk <79501315+Frisk0316@users.noreply.github.com> Date: Fri, 5 May 2023 20:52:47 +0800 Subject: [PATCH 2/3] Create 1068. Product Sales Analysis I # finished at 2023/05/05 # Runtime beats 36.8% --- 1068. Product Sales Analysis I | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 1068. Product Sales Analysis I diff --git a/1068. Product Sales Analysis I b/1068. Product Sales Analysis I new file mode 100644 index 0000000..012dbc0 --- /dev/null +++ b/1068. Product Sales Analysis I @@ -0,0 +1,79 @@ +/* +Problem Description: + ++-------------+-------+ +| Column Name | Type | ++-------------+-------+ +| sale_id | int | +| product_id | int | +| year | int | +| quantity | int | +| price | int | ++-------------+-------+ +(sale_id, year) is the primary key of this table. +product_id is a foreign key to Product table. +Each row of this table shows a sale on the product product_id in a certain year. +Note that the price is per unit. + + + +Table: Product + ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| product_id | int | +| product_name | varchar | ++--------------+---------+ +product_id is the primary key of this table. +Each row of this table indicates the product name of each product. + + + +Write an SQL query that reports the product_name, year, and price for each sale_id in the Sales table. + +Return the resulting table in any order. + +The query result format is in the following example. + + + +Example 1: + +Input: +Sales table: ++---------+------------+------+----------+-------+ +| sale_id | product_id | year | quantity | price | ++---------+------------+------+----------+-------+ +| 1 | 100 | 2008 | 10 | 5000 | +| 2 | 100 | 2009 | 12 | 5000 | +| 7 | 200 | 2011 | 15 | 9000 | ++---------+------------+------+----------+-------+ +Product table: ++------------+--------------+ +| product_id | product_name | ++------------+--------------+ +| 100 | Nokia | +| 200 | Apple | +| 300 | Samsung | ++------------+--------------+ +Output: ++--------------+-------+-------+ +| product_name | year | price | ++--------------+-------+-------+ +| Nokia | 2008 | 5000 | +| Nokia | 2009 | 5000 | +| Apple | 2011 | 9000 | ++--------------+-------+-------+ +Explanation: +From sale_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008. +From sale_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009. +From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011. + +*/ + +# Write your MySQL query statement below + +SELECT p.product_name, s.year, s.price +FROM sales s Left Join product p +ON p.product_id = s.product_id From c189d2d7a0c3f702f4d78c7bd9f227c92c736146 Mon Sep 17 00:00:00 2001 From: Frisk <79501315+Frisk0316@users.noreply.github.com> Date: Fri, 5 May 2023 22:48:21 +0800 Subject: [PATCH 3/3] Create 1581. Customer Who Visited but Did Not Make Any Transactions # finished at 2023.05.05 # Runtime beats 5.3% --- ... Visited but Did Not Make Any Transactions | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 1581. Customer Who Visited but Did Not Make Any Transactions diff --git a/1581. Customer Who Visited but Did Not Make Any Transactions b/1581. Customer Who Visited but Did Not Make Any Transactions new file mode 100644 index 0000000..09cfb2f --- /dev/null +++ b/1581. Customer Who Visited but Did Not Make Any Transactions @@ -0,0 +1,87 @@ +/* +Problem Description: + +Table: Visits + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| visit_id | int | +| customer_id | int | ++-------------+---------+ +visit_id is the primary key for this table. +This table contains information about the customers who visited the mall. + + + +Table: Transactions + ++----------------+---------+ +| Column Name | Type | ++----------------+---------+ +| transaction_id | int | +| visit_id | int | +| amount | int | ++----------------+---------+ +transaction_id is the primary key for this table. +This table contains information about the transactions made during the visit_id. + + + +Write a SQL query to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits. + +Return the result table sorted in any order. + +The query result format is in the following example. + + + +Example 1: + +Input: +Visits ++----------+-------------+ +| visit_id | customer_id | ++----------+-------------+ +| 1 | 23 | +| 2 | 9 | +| 4 | 30 | +| 5 | 54 | +| 6 | 96 | +| 7 | 54 | +| 8 | 54 | ++----------+-------------+ +Transactions ++----------------+----------+--------+ +| transaction_id | visit_id | amount | ++----------------+----------+--------+ +| 2 | 5 | 310 | +| 3 | 5 | 300 | +| 9 | 5 | 200 | +| 12 | 1 | 910 | +| 13 | 2 | 970 | ++----------------+----------+--------+ +Output: ++-------------+----------------+ +| customer_id | count_no_trans | ++-------------+----------------+ +| 54 | 2 | // visit_id is 7, 8 +| 30 | 1 | // visit_id is 4 +| 96 | 1 | // visit_id is 6 ++-------------+----------------+ +Explanation: +Customer with id = 23 visited the mall once and made one transaction during the visit with id = 12. +Customer with id = 9 visited the mall once and made one transaction during the visit with id = 13. +Customer with id = 30 visited the mall once and did not make any transactions. +Customer with id = 54 visited the mall three times. During 2 visits they did not make any transactions, and during one visit they made 3 transactions. +Customer with id = 96 visited the mall once and did not make any transactions. +As we can see, users with IDs 30 and 96 visited the mall one time without making any transactions. Also, user 54 visited the mall twice and did not make any transactions. + +*/ + +# Write your MySQL query statement below + +SELECT customer_id, COUNT(visit_id) as count_no_trans +FROM Visits +WHERE visit_id NOT IN (SELECT visit_id FROM Transactions) +GROUP BY customer_id