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
29 changes: 29 additions & 0 deletions sql/task1.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
-- 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.
with stat as
(select user_id, count(order_id) as cnt
from Orders
group by user_id)
select u.user_id, u.username, stat.cnt
from Users u
inner join stat
on u.user_id = o.user_id;

-- 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.
with rating as
(select product_id, AVG(rating) as rate
from Reviews
group by product_id)
select p.product_id, p.product_name, r.rate
from Products p
inner join rating r
on p.product_id=r.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.
with spent as
(select user_id, sum(total_amount) as amt
from Orders
group by user_id)
select u.user_id, u.username, s.amt
from Users u
inner join spent s
on u.user_id = s.user_id
order by s.amt desc
limit 5;
37 changes: 36 additions & 1 deletion sql/task2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,53 @@
-- 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 rating as
(select product_id, AVG(rating) as rate
from Reviews
group by product_id)
select p.product_id, p.product_name, r.rate
from Products p
inner join rating r
on p.product_id=r.product_id
where r.rate = (select max(rate) from rating);

-- 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
inner join Orders o
on u.user_id = o.user_id
inner join Order_Items oi
on o.order_id = oi.order_id
inner join Products p
on oi.product_id = p.product_id
group by u.user_id
having count(distinct p.category_id) = (select count(category_id) from Category)


-- 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.
with consec as
(select user_id, order_date,
LAG(order_date) over (partition by user_id order by order_date) as prev
from Orders)
select distinct c.user_id, u.username
from consec c
inner join Users u
on c.user_id = u.user_id
WHERE datediff(day, c.order_date, c.prev) = 1;
46 changes: 46 additions & 0 deletions sql/task3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,64 @@
-- 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.
select c.category_id, c.category_name,
sum(oi.quantity * oi.unit_price) as total
from Categories c
inner join Products p
on c.category_id = p.category_id
inner join Order_Items oi
on p.product_id = oi.product_id
inner join Orders o
on oi.order_id = o.order_id
group by c.category_id, c.category_name
order by total 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
inner join Orders o
on u.user_id = o.user_id
inner join Order_Items oi
on o.order_id = oi.order_id
inner join Products p
on oi.product_id = p.product_id
inner join Categories c
on p.category_id = c.category_id
where c.category_name = 'Toys & Games'
group by u.user_id
having count(distinct oi.product_id) = (select count(*)
from Products p
left join Categorues c
on p.category_id = c.category_id
where c.category_id = '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.
with ranking 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 ranking
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.
with tmp as
(select user_id,order_date,
LAG(order_date, 1) over (partition by user_id order by order_date) as prev,
LAG(order_date, 2) OVER (partition by user_id order by order_date) AS pprev
from Orders)
select distinct u.user_id, u.username
from Users u
left join tmp
on u.user_id = tmp.user_id
where DATEDIFF(day, order_date, prev) = 1 and DATEDIFF(day, pprev, prev) = 1;