diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..a4d93b499 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -12,3 +12,31 @@ -- 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 * +FROM products +WHERE category = 'Sports'; + +SELECT users.user_id, users.username, COUNT(orders.order_id) AS total_orders +FROM users +LEFT JOIN orders ON users.user_id = orders.user_id +GROUP BY users.user_id, users.username; + +SELECT products.product_id, products.product_name, AVG(ratings.rating) AS average_rating +FROM products +LEFT JOIN ratings ON products.product_id = ratings.product_id +GROUP BY products.product_id, products.product_name; + +SELECT users.user_id, users.username, SUM(order_details.total_price) AS total_amount_spent +FROM users +LEFT JOIN ( + SELECT orders.user_id, order_details.product_id, SUM(order_details.quantity * products.price) AS total_price + FROM orders + LEFT JOIN order_details ON orders.order_id = order_details.order_id + LEFT JOIN products ON order_details.product_id = products.product_id + GROUP BY orders.user_id, order_details.product_id +) AS order_details ON users.user_id = order_details.user_id +GROUP BY users.user_id, users.username +ORDER BY total_amount_spent DESC +LIMIT 5; + diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..33152ec79 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -16,4 +16,46 @@ -- 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 AvgProductRatings AS ( + SELECT products.product_id, products.product_name, AVG(ratings.rating) AS average_rating + FROM products + LEFT JOIN ratings ON products.product_id = ratings.product_id + GROUP BY products.product_id, products.product_name +) +SELECT product_id, product_name, average_rating +FROM AvgProductRatings +WHERE average_rating = ( + SELECT MAX(average_rating) FROM AvgProductRatings +); + + +SELECT users.user_id, users.username +FROM users +WHERE users.user_id IN ( + SELECT DISTINCT users.user_id + FROM users + JOIN orders ON users.user_id = orders.user_id + JOIN order_details ON orders.order_id = order_details.order_id + JOIN products ON order_details.product_id = products.product_id + GROUP BY users.user_id + HAVING COUNT(DISTINCT products.category) = ( + SELECT COUNT(DISTINCT category) FROM products + ) +); + +SELECT products.product_id, products.product_name +FROM products +LEFT JOIN ratings ON products.product_id = ratings.product_id +WHERE ratings.rating IS NULL; + +WITH UserOrdersWithDates AS ( + SELECT orders.user_id, orders.order_date, LAG(orders.order_date) OVER (PARTITION BY orders.user_id ORDER BY orders.order_date) AS prev_order_date + FROM orders +) +SELECT DISTINCT user_id, username +FROM users +JOIN UserOrdersWithDates ON users.user_id = UserOrdersWithDates.user_id +WHERE DATEDIFF(order_date, prev_order_date) = 1; + diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..821dc4018 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -17,3 +17,60 @@ -- 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 CategorySales AS ( + SELECT products.category_id, SUM(order_details.quantity * products.price) AS total_sales + FROM order_details + JOIN products ON order_details.product_id = products.product_id + GROUP BY products.category_id +) +SELECT categories.category_id, categories.category_name, COALESCE(total_sales, 0) AS total_sales +FROM categories +LEFT JOIN CategorySales ON categories.category_id = CategorySales.category_id +ORDER BY total_sales DESC +LIMIT 3; + +SELECT users.user_id, users.username +FROM users +WHERE users.user_id IN ( + SELECT DISTINCT users.user_id + FROM users + JOIN orders ON users.user_id = orders.user_id + JOIN order_details ON orders.order_id = order_details.order_id + JOIN products ON order_details.product_id = products.product_id + WHERE products.category = 'Toys & Games' + GROUP BY users.user_id + HAVING COUNT(DISTINCT products.product_id) = ( + SELECT COUNT(DISTINCT product_id) FROM products WHERE category = 'Toys & Games' + ) +); + +WITH MaxPricePerCategory AS ( + SELECT products.product_id, products.product_name, products.category_id, products.price, + ROW_NUMBER() OVER(PARTITION BY products.category_id ORDER BY products.price DESC) AS rn + FROM products +) +SELECT product_id, product_name, category_id, price +FROM MaxPricePerCategory +WHERE rn = 1; + +WITH UserOrdersWithDates AS ( + SELECT orders.user_id, orders.order_date, LAG(orders.order_date) OVER (PARTITION BY orders.user_id ORDER BY orders.order_date) AS prev_order_date + FROM orders +) +SELECT DISTINCT user_id, username +FROM users +JOIN UserOrdersWithDates ON users.user_id = UserOrdersWithDates.user_id +WHERE DATEDIFF(order_date, prev_order_date) = 1 +AND user_id IN ( + SELECT DISTINCT user_id + FROM UserOrdersWithDates + WHERE DATEDIFF(order_date, prev_order_date) = 1 + AND user_id IN ( + SELECT DISTINCT user_id + FROM UserOrdersWithDates + WHERE DATEDIFF(order_date, prev_order_date) = 1 + ) +); +