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
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"sqltools.connections": [
{
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"askForPassword": true,
"driver": "PostgreSQL",
"username": "Hersh",
"name": "ShopifyDB",
"database": "ShopifyDB"
}
]
}
17 changes: 5 additions & 12 deletions sql/task1.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
-- Problem 1: Retrieve all products in the Sports category
-- Problem 1: Retrieve all products in the Sports & Outdoors category
-- Write an SQL query to retrieve all products in a specific category.

-- 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.

-- 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.

-- 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 P.product_id, P.product_name, P.description, P.price
FROM Products AS P
JOIN Categories AS C ON P.category_id = C.category_id
WHERE C.category_name = 'Sports & Outdoors';
26 changes: 26 additions & 0 deletions sql/task10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- 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.

WITH ToysGamesProducts AS (
SELECT product_id
FROM Products
JOIN Categories ON Products.category_id = Categories.category_id
WHERE Categories.category_name = 'Toys & Games'
), UserOrdersForToysGames AS (
SELECT Orders.user_id, Order_Items.product_id
FROM Orders
JOIN Order_Items ON Orders.order_id = Order_Items.order_id
WHERE Order_Items.product_id IN (SELECT product_id FROM ToysGamesProducts)
GROUP BY Orders.user_id, Order_Items.product_id
), UserCount AS (
SELECT user_id, COUNT(*) AS product_count
FROM UserOrdersForToysGames
GROUP BY user_id
HAVING COUNT(*) = (SELECT COUNT(*) FROM ToysGamesProducts)
)

SELECT Users.user_id, Users.username
FROM Users
JOIN UserCount ON Users.user_id = UserCount.user_id;
25 changes: 25 additions & 0 deletions sql/task11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- 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 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
FROM
RankedProducts
WHERE
price_rank = 1;
42 changes: 42 additions & 0 deletions sql/task12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- 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 OrderedDates 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
Orders
),
ConsecutiveOrders AS (
SELECT
user_id,
order_date,
CASE
WHEN order_date = prev_order_date + INTERVAL '1 day' OR
order_date = next_order_date - INTERVAL '1 day' THEN 1
ELSE 0
END AS is_consecutive
FROM
OrderedDates
),
ConsecutiveGroups AS (
SELECT
user_id,
SUM(is_consecutive) OVER (PARTITION BY user_id ORDER BY order_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS consecutive_days
FROM
ConsecutiveOrders
)
SELECT DISTINCT
U.user_id,
U.username
FROM
Users U
JOIN
ConsecutiveGroups CG ON U.user_id = CG.user_id
WHERE
consecutive_days >= 2;
26 changes: 8 additions & 18 deletions sql/task2.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
-- Problem 5: Retrieve the products with the highest average rating
-- 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.
-- 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.

-- 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.

-- 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.

-- 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.
SELECT U.user_id, U.username, COUNT(O.order_id) AS total_orders
FROM Users AS U
LEFT JOIN Orders AS O ON U.user_id = O.user_id
GROUP BY U.user_id, U.username
ORDER BY U.user_id;
26 changes: 8 additions & 18 deletions sql/task3.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
-- Problem 9: Retrieve the top 3 categories with the highest total sales amount
-- 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.
-- 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.

-- 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.

-- 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.

-- 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.
SELECT P.product_id, P.product_name, AVG(R.rating) AS average_rating
FROM Products AS P
LEFT JOIN Reviews AS R ON P.product_id = R.product_id
GROUP BY P.product_id, P.product_name
ORDER BY P.product_id;
10 changes: 10 additions & 0 deletions sql/task4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- 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_spent
FROM Users AS U
JOIN Orders AS O ON U.user_id = O.user_id
GROUP BY U.user_id, U.username
ORDER BY total_spent DESC
LIMIT 5;
21 changes: 21 additions & 0 deletions sql/task5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Problem 5: Retrieve the products with the highest average rating
-- 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 ProductRatings AS (
SELECT
P.product_id,
P.product_name,
AVG(R.rating) AS average_rating
FROM Products AS P
LEFT JOIN Reviews AS R ON P.product_id = R.product_id
GROUP BY P.product_id, P.product_name
)

SELECT product_id, product_name, average_rating
FROM ProductRatings
WHERE average_rating = (
SELECT MAX(average_rating)
FROM ProductRatings
);
20 changes: 20 additions & 0 deletions sql/task6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- 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
JOIN
Orders O ON U.user_id = O.user_id
JOIN
Order_Items OI ON O.order_id = OI.order_id
JOIN
Products P ON OI.product_id = P.product_id
GROUP BY
U.user_id, U.username
HAVING
COUNT(DISTINCT P.category_id) = (SELECT COUNT(*) FROM Categories);
9 changes: 9 additions & 0 deletions sql/task7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- 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;
23 changes: 23 additions & 0 deletions sql/task8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- 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.

WITH OrderedUsers AS (
SELECT
U.user_id,
U.username,
O.order_date,
LAG(O.order_date) OVER (PARTITION BY U.user_id ORDER BY O.order_date) AS prev_order_date
FROM
Orders O
JOIN Users U ON O.user_id = U.user_id
)

SELECT DISTINCT
user_id,
username
FROM
OrderedUsers
WHERE
order_date = DATE_ADD(prev_order_date, INTERVAL 1 DAY);
20 changes: 20 additions & 0 deletions sql/task9.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Problem 9: Retrieve the top 3 categories with the highest total sales amount
-- 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_sales_amount
FROM
Categories C
JOIN
Products P ON C.category_id = P.category_id
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;
Loading