diff --git a/sql/task1.sql b/sql/task1.sql index 90de336ca..decdb5085 100644 --- a/sql/task1.sql +++ b/sql/task1.sql @@ -1,14 +1,66 @@ -- Problem 1: Retrieve all products in the Sports category -- Write an SQL query to retrieve all products in a specific category. +SELECT + product_id, + product_name, + description, + price, +FROM Products +INNER JOIN Categories ON Categories.category_id = Products.category_id +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 + user_id, + username, + COUNT(order_id) AS total_number_of_orders +FROM Users U +INNER JOIN Orders ON Orders.user_id = Users.user_id +GROUP BY U.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 + product_id, + product_name, + AVG(rating) AS average_rating +FROM Products P +INNER JOIN Reviews ON Reviews.product_id = Products.product_id +GROUP BY P.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. + +-- OLD VERSION: +/*SELECT + user_id, + username, + total_amount_spent +FROM ( + SELECT + user_id, + username, + SUM(total_amount) AS total_amount_spent + FROM Users U + INNER JOIN Orders ON Orders.user_id = Users.user_id + GROUP BY U.user_id +) AS T +ORDER BY total_amount_spent DESC +LIMIT 5;*/ + +SELECT + user_id, + username, + SUM(total_amount) AS total_amount_spent +FROM Users U +INNER JOIN Orders ON Orders.user_id = Users.user_id +GROUP BY U.user_id +ORDER BY total_amount_spent DESC +LIMIT 5; \ No newline at end of file diff --git a/sql/task2.sql b/sql/task2.sql index ad2596731..80ab96122 100644 --- a/sql/task2.sql +++ b/sql/task2.sql @@ -3,17 +3,70 @@ -- 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 + product_id, + product_name, + average_rating +FROM ( + SELECT + product_id, + product_name, + AVG(rating) AS average_rating + FROM Products P + INNER JOIN Reviews ON Reviews.product_id = Products.product_id + GROUP BY P.product_id; +) AS T +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 + user_id, + username +FROM ( + SELECT + user_id, + username, + COUNT(DISTINCT category_name) AS distinct_categories + FROM + Users U + JOIN Orders ON Orders.user_id = Users.user_id + JOIN Order_Items ON Order_Items.order_id = Orders.order_id + JOIN Products ON Products.product_id = Order_Items.product_id + JOIN Products ON Products.category_id = Categories.category_id + GROUP BY U.user_id +) AS T +WHERE distinct_categories = (SELECT + COUNT(DISTINCT category_id) + 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 + product_id, + product_name +FROM + Products +WHERE product_id NOT IN (SELECT + DISTINCT product_id + FROM Reviews) + -- 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. + +SELECT DISTINCT + user_id, + username +FROM + Users + JOIN Orders O1 ON user_id = O1.user_idF + JOIN Orders O2 ON user_id = O2.user_id AND O1.order_date = DATE_ADD(O2.order_date, INTERVAL 1 DAY); \ No newline at end of file diff --git a/sql/task3.sql b/sql/task3.sql index f078a9439..c9de9b821 100644 --- a/sql/task3.sql +++ b/sql/task3.sql @@ -3,17 +3,61 @@ -- 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_id, + category_name, + SUM(unit_price * quantity) AS total_sales_amount +FROM + Categories C + JOIN Products ON Categories.category_id = Products.product_id + JOIN Order_Items ON Products.product_id = Order_Items.product_id +GROUP BY C.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 + user_id, + username +FROM + Users +WHERE EXISTS ( + SELECT * + FROM + Products + JOIN Categories ON Products.category_id = Categories.category_id + WHERE + category_name = 'Toys & Games' + AND NOT EXISTS ( + SELECT * + FROM + Orders + JOIN Order_Items OI ON Orders.order_id = Order_Items.order_id + WHERE + Orders.user_id = Users.user_id AND Order_Items.product_id = Products.product_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 + product_id, + product_name, + category_id, + MAX(price) +FROM Products P +JOIN Categories ON Categories.category_id = Products.product_id +GROUP BY P.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. +-- Hint: You may need to use subqueries, joins, and window functions to solve this problem. \ No newline at end of file diff --git a/tests/test_sql_queries.py b/tests/test_sql_queries.py index 22b25d546..65dcf08d5 100644 --- a/tests/test_sql_queries.py +++ b/tests/test_sql_queries.py @@ -6,11 +6,11 @@ class TestSQLQueries(unittest.TestCase): def setUp(self): # Establish a connection to your test database self.conn = psycopg2.connect( - dbname='your_dbname', - user='your_username', - password='your_password', + dbname='postgres_db', + user='Postgres', + password='6292', host='your_host', - port='your_port' + port='5432' ) self.cur = self.conn.cursor()