diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..441bcb5bb 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,37 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +-- Utilize category ID to select desired products listed within the category +-- Category ID may be changed to better suit search parameters +SELECT * +FROM Products +WHERE Products.category_id = 8; -- 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(Order.order_id) AS total_orders +FROM Users +LEFT JOIN Orders ON Users.user_id = Orders.user_id +GROUP BY Users.user_id, Users.username; + + -- 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 +LEFT JOIN Reviews ON Products.product_id = Reviews.product_id +GROUP BY Products.product_id, Products.product_name; + -- 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 +JOIN Orders ON Users.user_id = Orders.user_id +GROUP BY Users.user_id, Users.username +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 ad2596731..74bfb2a2f 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -3,17 +3,40 @@ -- 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, Reviews.rating AS average_rating +FROM Products +LEFT JOIN Reviews ON Reviews.product_id = Products.product_id +WHERE Reviews.rating = (SELECT MAX(average_rating) +FROM (SELECT Products.product_id, Products.product_name, AVG(Reviews.rating) AS average_rating +FROM Products +LEFT JOIN Reviews ON Products.product_id = Reviews.product_id +GROUP BY Products.product_id, Products.product_name)); + -- 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 +JOIN Orders ON Users.user_id = Orders.order_id +JOIN Order_Items ON Orders.order_id = Order_Items.order_id +JOIN Products ON Order_Items.product_id = Products.product_id +JOIN Categories ON Products.category_id = Categories.category_id +GROUP BY Users.user_id +HAVING COUNT(DISTINCT Categories.category_id) = (SELECT COUNT(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. -- 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. + diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..654b411b9 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -3,15 +3,37 @@ -- 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 +FROM Categories +JOIN Products ON Categories.category_id = Products.category_id +JOIN Order_Items ON Products.product_id = Order_Items.product_id +JOIN Orders ON Order_Items.order_id = Orders.order_id +GROUP BY Categories.category_id, Categories.category_name +ORDER BY total_sales 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 +JOIN Orders ON Users.user_id = Orders.user_id +JOIN Order_Items ON Orders.order_id = Order_Items.order_id +JOIN Products ON Order_Items.product_id = Products.product_id +JOIN Categories ON Products.category_id = Categories.category_id +GROUP BY Users.user_id, Users.username +HAVING COUNT(DISTINCT Products.product_id) = (SELECT COUNT(DISTINCT product_id) FROM Products WHERE category_id = 5); + -- 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, price +FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY category_id ORDER BY price DESC) AS price_rows +FROM Products) AS Ranked_Prices +WHERE price_rows = 1; -- 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.