diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..56e4148cb 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,38 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +SELECT * --selecting all columns +FROM Products --from the Products table +WHERE category_id = 1; --where the category_id is 1 (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 --selecting the user_id, username, and total number of orders +FROM Users AS u -- from the Users table +LEFT JOIN Orders AS o ON u.user_id = o.user_id --left joining the Orders table on the user_id +GROUP BY u.user_id, u.username; --grouping by the user_id and 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 --selecting the product_id, product_name, and average rating +FROM Products AS p --from the Products table +LEFT JOIN Reviews AS r ON p.product_id = r.product_id --left joining the Reviews table on the product_id +ORDER BY p.product_id; --ordering by the product_id + -- 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 --selecting the user_id, username, and total amount spent +FROM Users AS u --from the Users table +JOIN Orders AS o ON u.user_id = o.user_id -- joining the Orders table on the user_id +GROUP BY u.user_id, u.username -- grouping by the user_id and username +ORDER BY total_amount_spent DESC -- ordering by the total amount spent in descending order +LIMIT 5; -- limiting the results to 5 diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..1c4dca6f2 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -3,17 +3,93 @@ -- 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. +-- Create a CTE to calculate the average rating for each product +-- Create a CTE to rank the products by average rating +WITH ProductRatings AS ( + SELECT + product_id, + AVG(rating) AS average_rating + FROM Reviews + GROUP BY product_id +) + + +, RankedProducts AS ( + + SELECT + p.product_id, + p.product_name, + pr.average_rating, + RANK() OVER (ORDER BY pr.average_rating DESC) AS rating_rank + FROM Products AS p + JOIN ProductRatings AS pr ON p.product_id = pr.product_id +) + + +SELECT -- Retrieve the products with the highest average rating + product_id, + product_name, + average_rating +FROM RankedProducts +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 users who have ordered from all categories +SELECT + u.user_id, + u.username +FROM + Users AS u +WHERE + -- Count the distinct categories from which the user has ordered + (SELECT COUNT(DISTINCT p.category_id) + FROM Orders AS o + JOIN Order_Items AS oi ON o.order_id = oi.order_id + JOIN Products AS p ON oi.product_id = p.product_id + WHERE o.user_id = u.user_id) = + -- Compare to the total number of categories available + (SELECT COUNT(*) 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 that have not received any reviews +SELECT p.product_id, p.product_name +FROM Products AS p +LEFT JOIN Reviews AS 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. + +-- Create a CTE to retrieve the users who have made orders on consecutive days +WITH OrderedUsers AS ( + SELECT + u.user_id, + u.username, + o.order_date, + LAG(o.order_date) OVER (PARTITION BY o.user_id ORDER BY o.order_date) AS previous_order_date + FROM + Orders AS o + JOIN Users AS u ON o.user_id = u.user_id +) + +SELECT DISTINCT -- Select users who have made consecutive orders on consecutive days + user_id, + username +FROM + OrderedUsers +WHERE + order_date = DATE(previous_order_date, '+1 day'); + + diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..0c1709498 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -3,17 +3,68 @@ -- 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 category details and calculate total sales amount +SELECT c.category_id, c.category_name, SUM(oi.quantity * oi.unit_price) AS total_sales_amount +FROM Categories c +JOIN Products p ON c.category_id = p.category_id -- Link products to their categories +JOIN Order_Items oi ON p.product_id = oi.product_id -- Get sales data for products +GROUP BY c.category_id, c.category_name -- Aggregate sales by category +ORDER BY total_sales_amount DESC -- Order categories by total sales +LIMIT 3; -- Return only the top 3 categories + + -- 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. +-- Find users who ordered all 'Toys & Games' products +SELECT u.user_id, u.username +FROM Users u +JOIN Orders o ON u.user_id = o.user_id -- Join orders to users +JOIN Order_Items oi ON o.order_id = oi.order_id -- Get items in those orders +JOIN Products p ON oi.product_id = p.product_id -- Link those items to products +WHERE p.category_id = ( + SELECT category_id FROM Categories WHERE category_name = 'Toys & Games' -- Target 'Toys & Games' category +) +GROUP BY u.user_id, u.username -- Group by user, Ensure user ordered all products in category +HAVING COUNT(DISTINCT p.product_id) = ( + SELECT COUNT(*) FROM Products WHERE category_id = ( + SELECT category_id FROM Categories 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. +-- Rank products by price within each category +WITH RankedProducts AS ( + SELECT product_id, product_name, category_id, price, + RANK() OVER (PARTITION BY category_id ORDER BY price DESC) AS price_rank + FROM Products +) + + +SELECT product_id, product_name, category_id, price -- Select highest-priced products in each category +FROM RankedProducts +WHERE price_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. +-- ee + +-- Identify consecutive order days for each user +WITH consecutivedays AS ( + SELECT user_id, order_date, DATE(order_date) - ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY order_date) AS grp FROM Orders +), +-- Count the length of each consecutive day streak +consec AS ( + SELECT user_id, COUNT(*) AS streak_length FROM consecutivedays GROUP BY user_id, grp HAVING COUNT(*) >= 3 +) +SELECT DISTINCT o.user_id, u.username FROM consec o JOIN Users u ON o.user_id = u.user_id;