From 67217c582753f3fc389cb127d3e8b7e4695784bb Mon Sep 17 00:00:00 2001 From: Selim Gilon <43852124+Seeeeeyo@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:40:54 -0500 Subject: [PATCH 1/3] Update task1.sql --- sql/task1.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..f567d11d2 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,34 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +SELECT * +FROM Products +INNER JOIN Categories ON Products.category_id = Categories.category_id +WHERE Categories.category_name LIKE '%Sports%'; -- 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 Users.user_id, Users.username, COUNT(Orders.order_id) AS total_orders +FROM Users +INNER JOIN Orders ON Users.user_id = Orders.user_id +GROUP BY Users.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 Products.product_id, Products.product_name, AVG(Reviews.rating) AS average_rating +FROM Products +INNER JOIN Reviews ON Products.product_id = Reviews.product_id +GROUP BY Products.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 Users.user_id, Users.username, SUM(Orders.total_amount) AS total_amount_spent +FROM Users +INNER JOIN Orders ON Users.user_id = Orders.user_id +GROUP BY Users.user_id +ORDER BY total_amount_spent DESC +LIMIT 5; From 396925591ad32ff52b9875ef3100e14255a0ee4b Mon Sep 17 00:00:00 2001 From: Selim Gilon <43852124+Seeeeeyo@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:41:07 -0500 Subject: [PATCH 2/3] Update task2.sql --- sql/task2.sql | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..1a94ab7e4 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -2,18 +2,42 @@ -- Write an SQL query to retrieve the products with the highest average rating. -- 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 Products.product_id, Products.product_name, AVG(Reviews.rating) AS average_rating +FROM Products +INNER JOIN Reviews ON Products.product_id = Reviews.product_id +GROUP BY Products.product_id +ORDER BY average_rating DESC +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. +SELECT Users.user_id, Users.username +FROM Users +INNER JOIN Orders ON Users.user_id = Orders.user_id +INNER JOIN Order_Items ON Orders.order_id = Order_Items.order_id +INNER JOIN Products ON Order_Items.product_id = Products.product_id +INNER JOIN Categories ON Products.category_id = Categories.category_id +GROUP BY Users.user_id +HAVING COUNT(DISTINCT Categories.category_id) = (SELECT COUNT(*) 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. -- 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 Products.product_id, Products.product_name +FROM Products +LEFT JOIN Reviews ON Products.product_id = Reviews.product_id +WHERE Reviews.review_id IS NULL; -- 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 Users.user_id, Users.username +FROM Users +INNER JOIN Orders ON Users.user_id = Orders.user_id +WHERE Orders.order_date = DATE_SUB(Orders.order_date, INTERVAL 1 DAY) From 787a06164d0c92f895412255603773985cdf354b Mon Sep 17 00:00:00 2001 From: Selim Gilon <43852124+Seeeeeyo@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:41:19 -0500 Subject: [PATCH 3/3] Update task3.sql --- sql/task3.sql | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..5ecda828f 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -2,18 +2,45 @@ -- Write an SQL query to retrieve the top 3 categories with the highest total sales amount. -- 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 Categories.category_id, Categories.category_name, SUM(Orders.total_amount) AS total_sales_amount +FROM Categories +INNER JOIN Products ON Categories.category_id = Products.category_id +INNER JOIN Order_Items ON Products.product_id = Order_Items.product_id +INNER JOIN Orders ON Order_Items.order_id = Orders.order_id +GROUP BY Categories.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 Users.user_id, Users.username +FROM Users +INNER JOIN Orders ON Users.user_id = Orders.user_id +INNER JOIN Order_Items ON Orders.order_id = Order_Items.order_id +INNER JOIN Products ON Order_Items.product_id = Products.product_id +INNER JOIN Categories ON Products.category_id = Categories.category_id +WHERE Categories.category_name LIKE '%Toys & Games%' +GROUP BY Users.user_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 Products.product_id, Products.product_name, Products.category_id, Products.price +FROM Products +INNER JOIN Categories ON Products.category_id = Categories.category_id +WHERE Products.price = (SELECT MAX(Products.price) FROM Products WHERE Products.category_id = Categories.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. +SELECT DISTINCT Users.user_id, Users.username +FROM Users +INNER JOIN Orders ON Users.user_id = Orders.user_id +WHERE Orders.order_date BETWEEN CURRENT_DATE - INTERVAL '3 DAYS' AND CURRENT_DATE - INTERVAL '1 DAY'; + +