This repository contains a set of MySQL exercises based on an e-commerce schema. The schema includes the following tables:
customersproductsordersorder_itemsreviews
Note: The seed data for these tables is provided in the
seed_data.sqlfile. You can focus on writing queries and solving the challenges without worrying about the schema or data insertion.
Task:
Write a SQL query that computes the total revenue for each product. Revenue is defined as the sum of (quantity × price) from all associated order items. Your result should include the product ID, product name, and total revenue, sorted in descending order of revenue.
Task:
Write a SQL query to identify the top 5 customers based on total spending. Calculate each customer's total spending by summing up the value of all their orders (i.e., the sum of quantity × price for each order item). Return the customer ID, name, and total spending.
Task:
Write a SQL query that calculates the average order value for each customer who has placed at least one order. The average order value should be computed as the total amount for an order divided by the number of orders for that customer. Return the customer ID, name, and average order value, sorted by average order value in descending order.
Task:
Write a SQL query to list all orders placed within the last 30 days. For each order, return the order ID, the customer’s name, the order date, and the order status. Use an appropriate join between the orders and customers tables. You may assume the current date is given by NOW().
Task:
Using a Common Table Expression (CTE), write a SQL query that returns, for each customer, a list of their orders ordered by order_date along with a running total of spending up to each order. Your output should include:
- Customer ID
- Order ID
- Order Date
- Order Total (the sum of
quantity×pricefor that order) - Running Total of spending for that customer
Task:
Write a SQL query to generate a summary for each product that includes:
- Product ID
- Product Name
- Average Review Rating
- Total Number of Reviews
Ensure that products without any reviews are also listed (you may display a count of 0 or NULL for the average rating). Sort the results by average rating in descending order (and then by the number of reviews if needed).
Task:
Write a SQL query to find all customers who have never placed an order. The output should include the customer ID and customer name.
Task:
Write a SQL statement (or a series of statements) to update the last_purchased column in the products table. For each product, set the value to the most recent order date from the orders that contain that product.
Hint: You’ll need to join the order_items and orders tables to retrieve the order date.
Task:
Simulate placing an order by writing a MySQL transaction that does the following:
- Deducts the ordered quantity from a product’s stock.
- Inserts a new record in the
orderstable. - Inserts one or more records in the
order_itemstable for that order. - Updates the product’s
last_purchasedtimestamp with the order date.
Ensure that your transaction includes error handling so that if any part fails, the entire transaction is rolled back. Provide the complete SQL code for the transaction.
Task:
Choose one of the queries you wrote above and answer the following:
- Describe how you would use the
EXPLAINstatement to analyze its performance. - Suggest any indexing or query refactoring improvements that could optimize its execution.
The following query is designed to calculate the total amount each customer has spent. It uses subqueries and the IN clause to aggregate order items, but it may not perform well on large datasets.
-- Unoptimized Query
SELECT
c.id AS customer_id,
c.name,
(SELECT SUM(oi.quantity * oi.price)
FROM order_items oi
WHERE oi.order_id IN (
SELECT o.id
FROM orders o
WHERE o.customer_id = c.id
)
) AS total_spent
FROM customers c
WHERE c.id IN (SELECT DISTINCT customer_id FROM orders)
ORDER BY total_spent DESC;