The document contains SQL query practice questions and solutions categorized into easy, medium, and hard levels. Each level includes various tasks such as retrieving user information, product details, and payment data. The solutions provide specific SQL commands to achieve the desired results for each question.
The document contains SQL query practice questions and solutions categorized into easy, medium, and hard levels. Each level includes various tasks such as retrieving user information, product details, and payment data. The solutions provide specific SQL commands to achieve the desired results for each question.
2. Get the names and prices of all products. 3. Find all orders placed after '2024-01-01'. 4. List all users who have a NULL email. 5. Show all payments sorted by amount in descending order. 6. Find all products with names that start with 'S'. 7. Get details of users who live in 'New York'. 8. Retrieve all payments made using 'Credit Card'.
MEDIUM LEVEL
1. List all products along with their category names.
2. Find the total number of orders placed by each user. 3. Retrieve the highest and lowest payment amounts. 4. Get the average rating for each product. 5. Find users who have placed more than 5 orders. 6. Show all users who have never placed an order. 7. Get the total revenue generated from all orders. 8. Find all orders where the quantity is between 5 and 10.
HARD LEVEL
1. Find the top 3 highest-selling products.
2. List all users who have placed an order but haven't made a payment yet. 3. Retrieve users who have placed orders for products in multiple categories. 4. Find users who have ordered products but have never left a review. 5. Get the most frequently used payment method. 6. List all products that have never been ordered. 7. Find users who have made payments greater than the average payment amount. 8. Get the category that has the highest number of products. SOLUTIONS
EASY LEVEL SOLUTIONS
1. SELECT * FROM Users;
2. SELECT product_name, price FROM Product; 3. SELECT * FROM Orders WHERE date_ordered > '2024-01-01'; 4. SELECT * FROM Users WHERE email IS NULL; 5. SELECT * FROM Payment ORDER BY amount DESC; 6. SELECT * FROM Product WHERE product_name LIKE 'S%'; 7. SELECT * FROM Address WHERE city = 'New York'; 8. SELECT * FROM Payment WHERE payment_method = 'Credit Card';
MEDIUM LEVEL SOLUTIONS
1. SELECT p.product_name, c.category_name FROM Product p JOIN Categories c ON
p.category_id = c.category_id; 2. SELECT user_id, COUNT(*) AS order_count FROM Orders GROUP BY user_id; 3. SELECT MAX(amount) AS highest_payment, MIN(amount) AS lowest_payment FROM Payment; 4. SELECT product_id, AVG(Rating) AS avg_rating FROM Reviews GROUP BY product_id; 5. SELECT user_id FROM Orders GROUP BY user_id HAVING COUNT(*) > 5; 6. SELECT * FROM Users WHERE user_id NOT IN (SELECT DISTINCT user_id FROM Orders); 7. SELECT SUM(amount) AS total_revenue FROM Payment; 8. SELECT * FROM Orders WHERE quantity BETWEEN 5 AND 10;
HARD LEVEL SOLUTIONS
1. SELECT product_id, SUM(quantity) AS total_sold FROM Orders GROUP BY product_id ORDER
BY total_sold DESC LIMIT 3; 2. SELECT user_id FROM Orders WHERE user_id NOT IN (SELECT user_id FROM Payment); 3. SELECT user_id FROM Orders GROUP BY user_id HAVING COUNT(DISTINCT product_id) > 1; 4. SELECT user_id FROM Orders WHERE user_id NOT IN (SELECT user_id FROM Reviews); 5. SELECT payment_method, COUNT(*) AS usage_count FROM Payment GROUP BY payment_method ORDER BY usage_count DESC LIMIT 1; 6. SELECT * FROM Product WHERE product_id NOT IN (SELECT DISTINCT product_id FROM Orders); 7. SELECT user_id FROM Payment WHERE amount > (SELECT AVG(amount) FROM Payment); 8. SELECT category_id, COUNT(*) AS product_count FROM Product GROUP BY category_id ORDER BY product_count DESC LIMIT 1;