SQL Data Analysis Test
Introduction:
In this exercise, we work with three related tables: Orders, Returns, and Users. We aim to extract
and analyze data from these tables using SQL queries.
- Orders: Contains details of each sales transaction such as order ID, customer, product, date,
quantity, price, total sales, priority, and region.
- Returns: Records order returns linked by the order ID from the Orders table.
- Users: Associates regions with specific managers.
Task 1: Which Manager Has the Highest Sales?
SQL Query:
SELECT u.manager, SUM(o.sales) AS total_sales
FROM Orders o
JOIN Users u ON o.region = u.region
GROUP BY u.manager
ORDER BY total_sales DESC
LIMIT 1;
Explanation:
1. We join the Orders and Users tables using the region field.
2. SUM(o.sales) calculates the total sales for each manager.
3. The results are grouped by the manager and sorted in descending order of total sales.
4. LIMIT 1 fetches the manager with the highest sales.
Task 2: Which is the 2nd Most Sold Product? How Much Revenue Did It Generate?
SQL Query:
SELECT o.product_id, SUM(o.quantity) AS total_quantity, SUM(o.sales) AS total_revenue
FROM Orders o
GROUP BY o.product_id
ORDER BY total_quantity DESC
LIMIT 1 OFFSET 1;
Explanation:
1. We group orders by product_id and calculate the total quantity sold and total revenue generated
for each product.
2. Sorting in descending order by total_quantity helps identify the most sold products.
3. LIMIT 1 OFFSET 1 returns the second-highest sold product and its total revenue.
Task 3: Who Are Our Top 3 Customers That Return the Most Products?
SQL Query:
SELECT o.customer_id, COUNT(r.order_id) AS total_returns
FROM Orders o
JOIN Returns r ON o.order_id = r.order_id
GROUP BY o.customer_id
ORDER BY total_returns DESC
LIMIT 3;
Explanation:
1. We join the Orders and Returns tables using order_id to identify orders that were returned.
2. Grouping by customer_id and counting returns for each customer.
3. The results are sorted by total returns in descending order and limited to the top 3 customers.
Task 4: Month-wise Breakdown of Products Sold and Sales Generated
SQL Query:
SELECT YEAR(o.order_date) AS order_year,
MONTH(o.order_date) AS order_month,
SUM(o.quantity) AS total_products_sold,
SUM(o.sales) AS total_sales
FROM Orders o
GROUP BY order_year, order_month
ORDER BY order_year, order_month;
Explanation:
1. Extract the year and month from the order_date.
2. Group by year and month to get the breakdown of sales.
3. Summing quantity gives the total products sold, while summing sales gives monthly sales totals.
Task 5: Additional Insights (Example Insight: Region with the Most Returns)
SQL Query:
SELECT o.region, COUNT(r.order_id) AS total_returns
FROM Orders o
JOIN Returns r ON o.order_id = r.order_id
GROUP BY o.region
ORDER BY total_returns DESC;
Explanation:
1. Join Orders and Returns tables using order_id.
2. Group by region to count the total returns per region.
3. Sorting by total returns in descending order helps identify regions with high return rates.
Task 5 (Alternative Insight): Average Order Value Per Region
SQL Query:
SELECT o.region,
AVG(o.sales) AS average_order_value
FROM Orders o
GROUP BY o.region
ORDER BY average_order_value DESC;
Explanation:
1. Use AVG(o.sales) to calculate the average sales amount per order, grouped by region.
2. Grouping by region allows identifying the regions with the highest average order value.
3. Sorting by average order value highlights high-revenue regions.