From a6e6302cbc522dbb5f72399e323ed766831313dc Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 20:08:09 -0500 Subject: [PATCH 1/8] Problem 1: Completed --- sql/task1.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..e4f866e1b 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,6 +1,17 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +SELECT + P.product_id, + P.product_name, + P.description, + P.price, +FROM + Products P, + Categories C +WHERE + P.category_id = C.category_id AND C.category_name = 'Sports & Outdoors'; + -- Problem 2: Retrieve the total number of orders for each user -- Write an SQL query to retrieve the total number of orders for each user. -- The result should include the user ID, username, and the total number of orders. From fb57b0a5f4e2ce24faee13490c5e5f422e3969d1 Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 21:02:35 -0500 Subject: [PATCH 2/8] Completed task 1 --- tests/test_sql_queries.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_sql_queries.py b/tests/test_sql_queries.py index 22b25d546..65dcf08d5 100644 --- a/tests/test_sql_queries.py +++ b/tests/test_sql_queries.py @@ -6,11 +6,11 @@ class TestSQLQueries(unittest.TestCase): def setUp(self): # Establish a connection to your test database self.conn = psycopg2.connect( - dbname='your_dbname', - user='your_username', - password='your_password', + dbname='postgres_db', + user='Postgres', + password='6292', host='your_host', - port='your_port' + port='5432' ) self.cur = self.conn.cursor() From 0544af7c2c8ce4c68ea2bc30401001a71bcbbd1a Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 21:02:55 -0500 Subject: [PATCH 3/8] Completed task 1 --- sql/task1.sql | 48 +++++++++++++++++++++++++++++++++++++++--------- sql/task2.sql | 19 +++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/sql/task1.sql b/sql/task1.sql index e4f866e1b..7c762d182 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -2,24 +2,54 @@ -- Write an SQL query to retrieve all products in a specific category. SELECT - P.product_id, - P.product_name, - P.description, - P.price, -FROM - Products P, - Categories C -WHERE - P.category_id = C.category_id AND C.category_name = 'Sports & Outdoors'; + product_id, + product_name, + description, + price, +FROM Products +INNER JOIN Categories ON Categories.user_id = Products.user_id +WHERE category_name = 'Sports & Outdoors'; -- Problem 2: Retrieve the total number of orders for each user -- Write an SQL query to retrieve the total number of orders for each user. -- The result should include the user ID, username, and the total number of orders. +SELECT + user_id, + username, + COUNT(order_id) AS total_number_of_orders +FROM Users U +INNER JOIN Orders ON Orders.user_id = Users.user_id +GROUP BY U.user_id; + -- Problem 3: Retrieve the average rating for each product -- Write an SQL query to retrieve the average rating for each product. -- The result should include the product ID, product name, and the average rating. +SELECT + product_id, + product_name, + AVG(rating) AS average_rating +FROM Products P +INNER JOIN Reviews ON Reviews.product_id = Products.product_id +GROUP BY P.product_id; + -- Problem 4: Retrieve the top 5 users with the highest total amount spent on orders -- Write an SQL query to retrieve the top 5 users with the highest total amount spent on orders. -- The result should include the user ID, username, and the total amount spent. + +SELECT + user_id, + username, + total_amount_spent +FROM ( + SELECT + user_id, + username, + SUM(total_amount) AS total_amount_spent + FROM Users U + INNER JOIN Orders ON Orders.user_id = Users.user_id + GROUP BY U.user_id +) AS T +ORDER BY total_amount_spent ASC +LIMIT 5; \ No newline at end of file diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..5f4acd20d 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -3,11 +3,30 @@ -- The result should include the product ID, product name, and the average rating. -- Hint: You may need to use subqueries or common table expressions (CTEs) to solve this problem. + +SELECT + product_id, + product_name, + average_rating +FROM ( + SELECT + product_id, + product_name, + AVG(rating) AS average_rating + FROM Products P + INNER JOIN Reviews ON Reviews.product_id = Products.product_id + GROUP BY P.product_id; +) AS T +ORDER BY average_rating ASC +LIMIT 1; + -- Problem 6: Retrieve the users who have made at least one order in each category -- Write an SQL query to retrieve the users who have made at least one order in each category. -- The result should include the user ID and username. -- Hint: You may need to use subqueries or joins to solve this problem. + + -- Problem 7: Retrieve the products that have not received any reviews -- Write an SQL query to retrieve the products that have not received any reviews. -- The result should include the product ID and product name. From 3b74cd081aa63b588c08b1bbc6beac496b1bac13 Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 21:58:25 -0500 Subject: [PATCH 4/8] Fixed mistake --- sql/task2.sql | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sql/task2.sql b/sql/task2.sql index 5f4acd20d..98aa3ca32 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -3,7 +3,6 @@ -- The result should include the product ID, product name, and the average rating. -- Hint: You may need to use subqueries or common table expressions (CTEs) to solve this problem. - SELECT product_id, product_name, @@ -17,7 +16,7 @@ FROM ( INNER JOIN Reviews ON Reviews.product_id = Products.product_id GROUP BY P.product_id; ) AS T -ORDER BY average_rating ASC +ORDER BY average_rating DESC LIMIT 1; -- Problem 6: Retrieve the users who have made at least one order in each category @@ -32,6 +31,15 @@ LIMIT 1; -- The result should include the product ID and product name. -- Hint: You may need to use subqueries or left joins to solve this problem. +SELECT + product_id, + product_name +FROM + Products +WHERE product_id NOT IN (SELECT + DISTINCT product_id + FROM Reviews) + -- Problem 8: Retrieve the users who have made consecutive orders on consecutive days -- Write an SQL query to retrieve the users who have made consecutive orders on consecutive days. -- The result should include the user ID and username. From e9291a593b1a29b6aa2be705e15c7a064cd7225a Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 21:58:34 -0500 Subject: [PATCH 5/8] Fixed mistake --- sql/task1.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/task1.sql b/sql/task1.sql index 7c762d182..82569301a 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -51,5 +51,5 @@ FROM ( INNER JOIN Orders ON Orders.user_id = Users.user_id GROUP BY U.user_id ) AS T -ORDER BY total_amount_spent ASC +ORDER BY total_amount_spent DESC LIMIT 5; \ No newline at end of file From 99676b50c7bea04cd68037de0ccbd2a23d791483 Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 23:16:04 -0500 Subject: [PATCH 6/8] Completed 11 problems --- sql/task1.sql | 15 +++++++++++++-- sql/task2.sql | 30 ++++++++++++++++++++++++++++-- sql/task3.sql | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 5 deletions(-) diff --git a/sql/task1.sql b/sql/task1.sql index 82569301a..decdb5085 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -7,7 +7,7 @@ SELECT description, price, FROM Products -INNER JOIN Categories ON Categories.user_id = Products.user_id +INNER JOIN Categories ON Categories.category_id = Products.category_id WHERE category_name = 'Sports & Outdoors'; -- Problem 2: Retrieve the total number of orders for each user @@ -38,7 +38,8 @@ GROUP BY P.product_id; -- Write an SQL query to retrieve the top 5 users with the highest total amount spent on orders. -- The result should include the user ID, username, and the total amount spent. -SELECT +-- OLD VERSION: +/*SELECT user_id, username, total_amount_spent @@ -52,4 +53,14 @@ FROM ( GROUP BY U.user_id ) AS T ORDER BY total_amount_spent DESC +LIMIT 5;*/ + +SELECT + user_id, + username, + SUM(total_amount) AS total_amount_spent +FROM Users U +INNER JOIN Orders ON Orders.user_id = Users.user_id +GROUP BY U.user_id +ORDER BY total_amount_spent DESC LIMIT 5; \ No newline at end of file diff --git a/sql/task2.sql b/sql/task2.sql index 98aa3ca32..f433ec7e5 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -24,7 +24,25 @@ LIMIT 1; -- The result should include the user ID and username. -- Hint: You may need to use subqueries or joins to solve this problem. - +SELECT + user_id, + username +FROM ( + SELECT + user_id, + username, + COUNT(DISTINCT category_name) AS distinct_categories + FROM + Users + JOIN Orders ON Orders.user_id = Users.user_id + JOIN Order_Items ON Order_Items.order_id = Orders.order_id + JOIN Products ON Products.product_id = Order_Items.product_id + JOIN Products ON Products.category_id = Categories.category_id + GROUP BY user_id +) AS T +WHERE distinct_categories = (SELECT + COUNT(DISTINCT category_id) + FROM Categories) -- Problem 7: Retrieve the products that have not received any reviews -- Write an SQL query to retrieve the products that have not received any reviews. @@ -43,4 +61,12 @@ WHERE product_id NOT IN (SELECT -- Problem 8: Retrieve the users who have made consecutive orders on consecutive days -- Write an SQL query to retrieve the users who have made consecutive orders on consecutive days. -- The result should include the user ID and username. --- Hint: You may need to use subqueries or window functions to solve this problem. \ No newline at end of file +-- Hint: You may need to use subqueries or window functions to solve this problem. + +SELECT DISTINCT + user_id, + username +FROM + Users + JOIN Orders O1 ON U.user_id = O1.user_id + JOIN Orders O2 ON U.user_id = O2.user_id AND O1.order_date = DATE_ADD(O2.order_date, INTERVAL 1 DAY); \ No newline at end of file diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..617181281 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -3,17 +3,61 @@ -- The result should include the category ID, category name, and the total sales amount. -- Hint: You may need to use subqueries, joins, and aggregate functions to solve this problem. +SELECT + category_id, + category_name, + SUM(unit_price * quantity) AS total_sales_amount +FROM + Categories + JOIN Products ON Categories.category_id = Products.product_id + JOIN Order_Items ON Products.product_id = Order_Items.product_id +GROUP BY category_id +ORDER BY total_sales_amount DESC +LIMIT 3; + + -- Problem 10: Retrieve the users who have placed orders for all products in the Toys & Games -- Write an SQL query to retrieve the users who have placed orders for all products in the Toys & Games -- The result should include the user ID and username. -- Hint: You may need to use subqueries, joins, and aggregate functions to solve this problem. +SELECT + user_id, + username +FROM + Users +WHERE EXISTS ( + SELECT * + FROM + Products + JOIN Categories ON Products.category_id = Categories.category_id + WHERE + category_name = 'Toys & Games' + AND NOT EXISTS ( + SELECT * + FROM + Orders + JOIN Order_Items OI ON Orders.order_id = Order_Items.order_id + WHERE + Orders.user_id = Users.user_id AND Order_Items.product_id = Products.product_id + ) +) + -- Problem 11: Retrieve the products that have the highest price within each category -- Write an SQL query to retrieve the products that have the highest price within each category. -- The result should include the product ID, product name, category ID, and price. -- Hint: You may need to use subqueries, joins, and window functions to solve this problem. +SELECT + product_id, + product_name, + category_id, + MAX(price) +FROM Products +JOIN Categories ON Categories.category_id = Products.product_id +GROUP BY category_id; + -- Problem 12: Retrieve the users who have placed orders on consecutive days for at least 3 days -- Write an SQL query to retrieve the users who have placed orders on consecutive days for at least 3 days. -- The result should include the user ID and username. --- Hint: You may need to use subqueries, joins, and window functions to solve this problem. +-- Hint: You may need to use subqueries, joins, and window functions to solve this problem. \ No newline at end of file From 751aa316cc5a4e790522be7844079423d0d97d8d Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 23:17:50 -0500 Subject: [PATCH 7/8] Fixed typo --- sql/task2.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/task2.sql b/sql/task2.sql index f433ec7e5..6d4da189d 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -68,5 +68,5 @@ SELECT DISTINCT username FROM Users - JOIN Orders O1 ON U.user_id = O1.user_id - JOIN Orders O2 ON U.user_id = O2.user_id AND O1.order_date = DATE_ADD(O2.order_date, INTERVAL 1 DAY); \ No newline at end of file + JOIN Orders O1 ON user_id = O1.user_id + JOIN Orders O2 ON user_id = O2.user_id AND O1.order_date = DATE_ADD(O2.order_date, INTERVAL 1 DAY); \ No newline at end of file From cb8d4a4c28b1ada862c49c16370fc4f5892c9793 Mon Sep 17 00:00:00 2001 From: Marwa Khalid Date: Mon, 29 Jan 2024 23:38:23 -0500 Subject: [PATCH 8/8] Fixed convention --- sql/task2.sql | 6 +++--- sql/task3.sql | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sql/task2.sql b/sql/task2.sql index 6d4da189d..80ab96122 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -33,12 +33,12 @@ FROM ( username, COUNT(DISTINCT category_name) AS distinct_categories FROM - Users + Users U JOIN Orders ON Orders.user_id = Users.user_id JOIN Order_Items ON Order_Items.order_id = Orders.order_id JOIN Products ON Products.product_id = Order_Items.product_id JOIN Products ON Products.category_id = Categories.category_id - GROUP BY user_id + GROUP BY U.user_id ) AS T WHERE distinct_categories = (SELECT COUNT(DISTINCT category_id) @@ -68,5 +68,5 @@ SELECT DISTINCT username FROM Users - JOIN Orders O1 ON user_id = O1.user_id + JOIN Orders O1 ON user_id = O1.user_idF JOIN Orders O2 ON user_id = O2.user_id AND O1.order_date = DATE_ADD(O2.order_date, INTERVAL 1 DAY); \ No newline at end of file diff --git a/sql/task3.sql b/sql/task3.sql index 617181281..c9de9b821 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -8,10 +8,10 @@ SELECT category_name, SUM(unit_price * quantity) AS total_sales_amount FROM - Categories + Categories C JOIN Products ON Categories.category_id = Products.product_id JOIN Order_Items ON Products.product_id = Order_Items.product_id -GROUP BY category_id +GROUP BY C.category_id ORDER BY total_sales_amount DESC LIMIT 3; @@ -53,9 +53,9 @@ SELECT product_name, category_id, MAX(price) -FROM Products +FROM Products P JOIN Categories ON Categories.category_id = Products.product_id -GROUP BY category_id; +GROUP BY P.category_id; -- Problem 12: Retrieve the users who have placed orders on consecutive days for at least 3 days -- Write an SQL query to retrieve the users who have placed orders on consecutive days for at least 3 days.