diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..5d1de9500 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,57 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +SELECT * FROM shopify.product_data WHERE 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 + O.user_id, + U.username, + COUNT(O.order_id) AS total_orders +FROM + shopify.user_data U +INNER JOIN + shopify.order_data 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 * FROM shopify.review_data; + +SELECT + P.product_id, + P.product_name, + AVG(R.rating) AS average_rating +FROM + shopify.product_data P +LEFT JOIN + shopify.review_data 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 + shopify.user_data U +JOIN + shopify.order_data O ON U.user_id = O.user_id +GROUP BY + U.user_id, U.username +ORDER BY + total_amount_spent DESC, + username +LIMIT 5; diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..f3eee1a0b 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -3,17 +3,89 @@ -- 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 ProductAverageRating AS ( + SELECT + P.product_id, + P.product_name, + AVG(R.rating) AS average_rating + FROM + shopify.product_data P + LEFT JOIN + shopify.review_data R ON P.product_id = R.product_id + GROUP BY + P.product_id, P.product_name +) +SELECT + product_id, + product_name, + average_rating +FROM ( + SELECT + *, + RANK() OVER (ORDER BY average_rating DESC) AS ranking + FROM + ProductAverageRating +) AS RankedProducts +WHERE + ranking = 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 + shopify.user_data U +JOIN + shopify.order_data O ON U.user_id = O.user_id +JOIN + shopify.order_items_data OI ON O.order_id = OI.order_id +JOIN + shopify.product_data P ON OI.product_id = P.product_id +JOIN + shopify.category_data C ON P.category_id = C.category_id +GROUP BY + U.user_id, U.username, C.category_id +HAVING + COUNT(DISTINCT C.category_id) = (SELECT COUNT(*) FROM shopify.category_data); + -- 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 + shopify.product_data P +LEFT JOIN + shopify.review_data 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 + shopify.order_data +) +SELECT DISTINCT + U.user_id, + U.username +FROM + shopify.user_data U +JOIN + UserConsecutiveOrders UCO ON U.user_id = UCO.user_id +WHERE + UCO.order_date = UCO.prev_order_date + INTERVAL 1 DAY; diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..c9bbed810 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -3,17 +3,105 @@ -- 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(O.total_amount), 0) AS total_sales_amount +FROM + shopify.category_data C +LEFT JOIN + shopify.product_data P ON C.category_id = P.category_id +LEFT JOIN + shopify.order_items_data OI ON P.product_id = OI.product_id +LEFT JOIN + shopify.order_data O ON OI.order_id = O.order_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. +SELECT + U.user_id, + U.username +FROM + shopify.user_data U +JOIN + shopify.order_data O ON U.user_id = O.user_id +JOIN + shopify.order_items_data OI ON O.order_id = OI.order_id +JOIN + shopify.product_data P ON OI.product_id = P.product_id +JOIN + shopify.category_data C ON P.category_id = C.category_id +WHERE + C.category_name = 'Toys & Games' +GROUP BY + U.user_id, U.username +HAVING + COUNT(DISTINCT P.product_id) = (SELECT COUNT(*) FROM shopify.product_data WHERE category_id = + (SELECT category_id FROM shopify.category_data WHERE category_name = 'Toys & Games')); + -- 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 * FROM shopify.product_data + +WITH RankedProducts AS ( + SELECT + product_id, + product_name, + category_id, + price, + ROW_NUMBER() OVER (PARTITION BY category_id ORDER BY price DESC) AS rn + FROM + shopify.product_data +) +SELECT + product_id, + product_name, + category_id, + price +FROM + RankedProducts +WHERE + rn = 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. + +WITH UserConsecutiveOrders AS ( + SELECT + user_id, + order_date, + LAG(order_date) OVER (PARTITION BY user_id ORDER BY order_date) AS prev_order_date, + LEAD(order_date) OVER (PARTITION BY user_id ORDER BY order_date) AS next_order_date + FROM + shopify.order_data +) +SELECT DISTINCT + U.user_id, + U.username +FROM + shopify.user_data U +JOIN + UserConsecutiveOrders UCO ON U.user_id = UCO.user_id +WHERE + ( + (UCO.order_date = UCO.prev_order_date + INTERVAL 1 DAY AND UCO.order_date = UCO.next_order_date - INTERVAL 1 DAY) OR + (UCO.order_date = UCO.prev_order_date + INTERVAL 1 DAY AND UCO.next_order_date IS NULL) OR + (UCO.prev_order_date IS NULL AND UCO.order_date = UCO.next_order_date - INTERVAL 1 DAY) + ) +GROUP BY + U.user_id, U.username +HAVING + COUNT(DISTINCT UCO.order_date) >= 3;