From 1937b04cfc7dd0326cf23ccb58126b8649376bd5 Mon Sep 17 00:00:00 2001 From: Aina Merchant Date: Mon, 29 Jan 2024 18:32:57 -0500 Subject: [PATCH 1/3] Task 1 --- sql/task1.sql | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..38a4b8251 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,27 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +SELECT * +FROM Products +WHERE category_id = (SELECT category_id FROM Categories WHERE category_name = '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 u.user_id, u.username, COUNT(o.order_id) AS total_orders +FROM Users u LEFT JOIN Orders o ON u.user_id = o.user_id +GROUP BY u.user_id, u.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 p.product_id, p.product_name, AVG(r.rating) AS average_rating +FROM Products p LEFT JOIN Reviews r ON p.product_id = r.product_id +GROUP BY p.product_id, p.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 TOP 5 u.user_id, u.username, SUM(o.total_amount) AS total_amount_spent +FROM Users u LEFT JOIN Orders o ON u.user_id = o.user_id +GROUP BY u.user_id, u.username +ORDER BY total_amount_spent DESC; \ No newline at end of file From a1d7a487e5d62db8651065f7648dc129e959928c Mon Sep 17 00:00:00 2001 From: Aina Merchant Date: Mon, 29 Jan 2024 20:19:06 -0500 Subject: [PATCH 2/3] Modified task1, completed task 2 --- sql/task1.sql | 6 +++--- sql/task2.sql | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/sql/task1.sql b/sql/task1.sql index 38a4b8251..91983a0f4 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -7,9 +7,9 @@ WHERE category_id = (SELECT category_id FROM Categories WHERE category_name = 'S -- 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 u.user_id, u.username, COUNT(o.order_id) AS total_orders -FROM Users u LEFT JOIN Orders o ON u.user_id = o.user_id -GROUP BY u.user_id, u.username; +SELECT Users.user_id, Users.username, + (SELECT COUNT(*) FROM Orders WHERE Orders.user_id = Users.user_id) AS total_orders +FROM Users; -- Problem 3: Retrieve the average rating for each product -- Write an SQL query to retrieve the average rating for each product. diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..1a969c1c9 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -2,18 +2,64 @@ -- 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. +WITH AvgRatings AS ( + SELECT + p.product_id, + p.product_name, + AVG(r.rating) AS average_rating, + RANK() OVER (ORDER BY AVG(r.rating) DESC) AS rating_rank + FROM + Products p + LEFT JOIN + Reviews r ON p.product_id = r.product_id + GROUP BY + p.product_id, p.product_name +) + +SELECT product_id, product_name, average_rating +FROM AvgRatings +WHERE rating_rank = 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 u.user_id, u.username +FROM Users u +WHERE NOT EXISTS ( + SELECT DISTINCT category_id + FROM Categories + EXCEPT + SELECT DISTINCT p.category_id + FROM Products p + JOIN Order_Items oi ON p.product_id = oi.product_id + JOIN Orders o ON oi.order_id = o.order_id + WHERE u.user_id = o.user_id +); + -- 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 p.product_id, p.product_name +FROM Products p +LEFT JOIN Reviews r ON p.product_id = r.product_id +WHERE r.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. +WITH UserConsecutiveOrders AS ( + SELECT + user_id, + order_date, + LAG(order_date) OVER (PARTITION BY user_id ORDER BY order_date) AS prev_order_date + FROM Orders +) +SELECT DISTINCT u.user_id, u.username +FROM Users u +JOIN UserConsecutiveOrders uco ON u.user_id = uco.user_id +WHERE DATEDIFF(day, uco.prev_order_date, uco.order_date) = 1 +ORDER BY u.user_id; \ No newline at end of file From 87824d4ca3e29136fdda9afb9572fea3c3e1aa8d Mon Sep 17 00:00:00 2001 From: Aina Merchant Date: Mon, 29 Jan 2024 23:06:00 -0500 Subject: [PATCH 3/3] Modified tasks 1 and 2 and Completed Task 3 --- sql/task1.sql | 11 ++++++++--- sql/task2.sql | 8 +++++++- sql/task3.sql | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/sql/task1.sql b/sql/task1.sql index 91983a0f4..df3b7f3f5 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,12 +1,14 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. + SELECT * FROM Products -WHERE category_id = (SELECT category_id FROM Categories WHERE category_name = 'Sports'); +WHERE category_id = (SELECT category_id FROM Categories 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 Users.user_id, Users.username, (SELECT COUNT(*) FROM Orders WHERE Orders.user_id = Users.user_id) AS total_orders FROM Users; @@ -14,6 +16,7 @@ FROM Users; -- 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 p.product_id, p.product_name, AVG(r.rating) AS average_rating FROM Products p LEFT JOIN Reviews r ON p.product_id = r.product_id GROUP BY p.product_id, p.product_name; @@ -21,7 +24,9 @@ GROUP BY p.product_id, p.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 TOP 5 u.user_id, u.username, SUM(o.total_amount) AS total_amount_spent + +SELECT u.user_id, u.username, SUM(o.total_amount) AS total_amount_spent FROM Users u LEFT JOIN Orders o ON u.user_id = o.user_id GROUP BY u.user_id, u.username -ORDER BY total_amount_spent DESC; \ No newline at end of file +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 1a969c1c9..086d4eb4f 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -2,6 +2,8 @@ -- 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. + +-- Common Table Expression that finds the average rating for each product and ranks them WITH AvgRatings AS ( SELECT p.product_id, @@ -24,6 +26,7 @@ WHERE rating_rank = 1; -- 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 u.user_id, u.username FROM Users u WHERE NOT EXISTS ( @@ -37,11 +40,11 @@ WHERE NOT EXISTS ( WHERE u.user_id = o.user_id ); - -- 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 p.product_id, p.product_name FROM Products p LEFT JOIN Reviews r ON p.product_id = r.product_id @@ -51,6 +54,8 @@ WHERE r.review_id IS NULL; -- 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. + +-- Common Table Expression that finds the previous order date for each user WITH UserConsecutiveOrders AS ( SELECT user_id, @@ -58,6 +63,7 @@ WITH UserConsecutiveOrders AS ( LAG(order_date) OVER (PARTITION BY user_id ORDER BY order_date) AS prev_order_date FROM Orders ) + SELECT DISTINCT u.user_id, u.username FROM Users u JOIN UserConsecutiveOrders uco ON u.user_id = uco.user_id diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..1cd3555b2 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -3,17 +3,66 @@ -- 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 c.category_id, c.category_name, + COALESCE(SUM(oi.quantity * oi.unit_price), 0) AS total_sales_amount +FROM Categories c +LEFT JOIN Products p ON c.category_id = p.category_id +LEFT JOIN Order_Items oi ON p.product_id = oi.product_id +GROUP BY c.category_id, c.category_name +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. +-- Common Table Expression that gets the products belonging to the Toys & Games category +WITH ToysAndGamesProducts AS ( + SELECT product_id + FROM Products + WHERE category_id = (SELECT category_id FROM Categories WHERE category_name = 'Toys & Games') +) +SELECT u.user_id, u.username +FROM Users u +JOIN Orders o ON u.user_id = o.user_id +JOIN Order_Items oi ON o.order_id = oi.order_id +WHERE oi.product_id IN (SELECT product_id FROM ToysAndGamesProducts) +GROUP BY u.user_id, u.username +HAVING COUNT(DISTINCT oi.product_id) = (SELECT COUNT(*) FROM ToysAndGamesProducts); + -- 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. +-- Common Table Expression that ranks on the basis of price for each product category +WITH HighestPrice AS ( + SELECT product_id, product_name, category_id, price, RANK() OVER (PARTITION BY category_id ORDER BY price DESC) AS rank + FROM Products +) + +SELECT product_id, product_name, category_id, price +FROM HighestPrice +WHERE rank = 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. -- The result should include the user ID and username. -- Hint: You may need to use subqueries, joins, and window functions to solve this problem. + +-- Common Table Expression that calculates the start and end dates of three consecutive orders for each user +WITH UserOrderDates AS ( + SELECT + user_id, + order_date AS start_date, + LEAD(order_date, 2) OVER (PARTITION BY user_id ORDER BY order_date) AS end_date + FROM Orders +) + +SELECT DISTINCT + uod.user_id, + u.username +FROM UserOrderDates uod +JOIN Users u ON uod.user_id = u.user_id +WHERE DATEDIFF(uod.end_date, uod.start_date) >= 2;