Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions sql/task1.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
-- 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 & 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;

-- 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 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
LIMIT 5;
54 changes: 53 additions & 1 deletion sql/task2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,69 @@
-- 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,
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.
-- 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,
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;
49 changes: 49 additions & 0 deletions sql/task3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;