SQL_Data_Analysis_Test
SQL_Data_Analysis_Test
Introduction:
In this exercise, we work with three related tables: Orders, Returns, and Users. We aim to extract
- Orders: Contains details of each sales transaction such as order ID, customer, product, date,
- Returns: Records order returns linked by the order ID from the Orders table.
SQL Query:
FROM Orders o
GROUP BY u.manager
LIMIT 1;
Explanation:
1. We join the Orders and Users tables using the region field.
3. The results are grouped by the manager and sorted in descending order of total sales.
SQL Query:
FROM Orders o
GROUP BY o.product_id
LIMIT 1 OFFSET 1;
Explanation:
1. We group orders by product_id and calculate the total quantity sold and total revenue generated
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:
FROM Orders o
GROUP BY o.customer_id
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.
SQL Query:
MONTH(o.order_date) AS order_month,
SUM(o.quantity) AS total_products_sold,
SUM(o.sales) AS total_sales
FROM Orders o
Explanation:
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:
FROM Orders o
GROUP BY o.region
3. Sorting by total returns in descending order helps identify regions with high return rates.
SQL Query:
SELECT o.region,
AVG(o.sales) AS average_order_value
FROM Orders o
GROUP BY o.region
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.