Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shopify data engineering internship challenge 2024 #217

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions sql/task1.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
-- Problem 1: Retrieve all products in the Sports category
-- Write an SQL query to retrieve all products in a specific category.
SELECT *
FROM Products
INNER JOIN Categories ON Products.category_id = Categories.category_id
WHERE Categories.category_name LIKE '%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 Users.user_id, Users.username, COUNT(Orders.order_id) AS total_orders
FROM Users
INNER JOIN Orders ON Users.user_id = Orders.user_id
GROUP BY Users.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.
SELECT Products.product_id, Products.product_name, AVG(Reviews.rating) AS average_rating
FROM Products
INNER JOIN Reviews ON Products.product_id = Reviews.product_id
GROUP BY Products.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 Users.user_id, Users.username, SUM(Orders.total_amount) AS total_amount_spent
FROM Users
INNER JOIN Orders ON Users.user_id = Orders.user_id
GROUP BY Users.user_id
ORDER BY total_amount_spent DESC
LIMIT 5;
26 changes: 25 additions & 1 deletion sql/task2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@
-- 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.
SELECT Products.product_id, Products.product_name, AVG(Reviews.rating) AS average_rating
FROM Products
INNER JOIN Reviews ON Products.product_id = Reviews.product_id
GROUP BY Products.product_id
ORDER BY average_rating DESC
LIMIT 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.user_id, Users.username
FROM Users
INNER JOIN Orders ON Users.user_id = Orders.user_id
INNER JOIN Order_Items ON Orders.order_id = Order_Items.order_id
INNER JOIN Products ON Order_Items.product_id = Products.product_id
INNER JOIN Categories ON Products.category_id = Categories.category_id
GROUP BY Users.user_id
HAVING COUNT(DISTINCT Categories.category_id) = (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.product_id, Products.product_name
FROM Products
LEFT JOIN Reviews ON Products.product_id = Reviews.product_id
WHERE Reviews.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.
SELECT DISTINCT Users.user_id, Users.username
FROM Users
INNER JOIN Orders ON Users.user_id = Orders.user_id
WHERE Orders.order_date = DATE_SUB(Orders.order_date, INTERVAL 1 DAY)
27 changes: 27 additions & 0 deletions sql/task3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,45 @@
-- 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 Categories.category_id, Categories.category_name, SUM(Orders.total_amount) AS total_sales_amount
FROM Categories
INNER JOIN Products ON Categories.category_id = Products.category_id
INNER JOIN Order_Items ON Products.product_id = Order_Items.product_id
INNER JOIN Orders ON Order_Items.order_id = Orders.order_id
GROUP BY Categories.category_id
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 Users.user_id, Users.username
FROM Users
INNER JOIN Orders ON Users.user_id = Orders.user_id
INNER JOIN Order_Items ON Orders.order_id = Order_Items.order_id
INNER JOIN Products ON Order_Items.product_id = Products.product_id
INNER JOIN Categories ON Products.category_id = Categories.category_id
WHERE Categories.category_name LIKE '%Toys & Games%'
GROUP BY Users.user_id


-- 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 Products.product_id, Products.product_name, Products.category_id, Products.price
FROM Products
INNER JOIN Categories ON Products.category_id = Categories.category_id
WHERE Products.price = (SELECT MAX(Products.price) FROM Products WHERE Products.category_id = Categories.category_id);

-- 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 DISTINCT Users.user_id, Users.username
FROM Users
INNER JOIN Orders ON Users.user_id = Orders.user_id
WHERE Orders.order_date BETWEEN CURRENT_DATE - INTERVAL '3 DAYS' AND CURRENT_DATE - INTERVAL '1 DAY';