diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..8e12fea85 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,29 @@ -- 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.price FROM Products p JOIN Categories c ON p.category_id = c.category_id WHERE c.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 +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 +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 u.user_id, u.username, SUM(o.total_amount) AS total_amount_spent +FROM Users u +JOIN Orders o ON u.user_id = o.user_id +GROUP BY u.user_id, u.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..91aaf7bd3 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -2,18 +2,55 @@ -- 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 avg_rating AS ( + SELECT product_id, AVG(rating) AS average_rating + FROM Reviews + GROUP BY product_id; +) +SELECT p.product_id, p.product_name, ar.average_rating +FROM Products p +JOIN avg_rating ar ON p.product_id = ar.product_id +ORDER BY ar.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 u.user_id, u.username +FROM Users u +WHERE NOT EXISTS ( + SELECT c.category_id + FROM Categories c + WHERE NOT EXISTS ( + SELECT o.order_id + FROM Orders o + JOIN Order_Items oi ON o.order_id = oi.order_id + JOIN Products p ON oi.product_id = p.product_id + WHERE o.user_id = u.user_id AND p.category_id = c.category_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.product_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 ConsecutiveOrders 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 ConsecutiveOrders co ON u.user_id = co.user_id +WHERE DATEDIFF(co.order_date, co.prev_order_date) = 1; diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..d0f32ca08 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -2,18 +2,67 @@ -- 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. +WITH CategorySales AS ( + SELECT c.category_id, c.category_name, SUM(oi.quantity * oi.unit_price) AS total_sales + FROM Categories c + JOIN Products p ON c.category_id = p.category_id + JOIN Order_Items oi ON p.product_id = oi.product_id + GROUP BY c.category_id, c.category_name +) +SELECT category_id, category_name, total_sales +FROM CategorySales +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 u.user_id, u.username +FROM Users u +WHERE NOT EXISTS ( + SELECT p.product_id + FROM Products p + JOIN Categories c ON p.category_id = c.category_id + WHERE c.category_name = 'Toys & Games' + AND NOT EXISTS ( + SELECT oi.order_id + FROM Orders o + JOIN Order_Items oi ON o.order_id = oi.order_id + WHERE o.user_id = u.user_id + AND oi.product_id = p.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. +WITH MaxPrices AS ( + SELECT category_id, MAX(price) AS max_price + FROM Products + GROUP BY category_id +) +SELECT p.product_id, p.product_name, p.category_id, p.price +FROM Products p +JOIN MaxPrices mp ON p.category_id = mp.category_id AND p.price = mp.max_price; -- 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. +WITH ConsecutiveOrders AS ( + SELECT user_id, order_date, + LAG(order_date, 1) OVER (PARTITION BY user_id ORDER BY order_date) AS prev_order_date, + LAG(order_date, 2) OVER (PARTITION BY user_id ORDER BY order_date) AS prev_prev_order_date + FROM Orders +), +ConsecutiveDays AS ( + SELECT user_id + FROM ConsecutiveOrders + WHERE DATEDIFF(order_date, prev_order_date) = 1 + AND DATEDIFF(prev_order_date, prev_prev_order_date) = 1 +) +SELECT DISTINCT u.user_id, u.username +FROM Users u +JOIN ConsecutiveDays cd ON u.user_id = cd.user_id;