Data Analyst SQL Test
Data:
The schema for the Dataset:
Tasks:
➢ Task 1: Which Manager has the highest Sales?
❖ CODE:
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 on the
Region field, as this links the sales data to the
managers responsible for each region.
2. We use SUM(o.Sales) to calculate the total sales
per manager.
3. We group the results by the manager to get
individual sales totals.
4. Finally, we order by Total_Sales in descending
order and limit the result to 1 to get the manager
with the highest sales.
➢Task 2: Which is the 2nd most sold Product? How
much revenue did it generate?
❖ CODE:
SELECT o.Product_Name,
SUM(o.Quantity_ordered_new) AS Total_Quantity,
SUM(o.Sales) AS Revenue
FROM Orders o
GROUP BY o.Product_Name
ORDER BY Total_Quantity DESC
LIMIT 1 OFFSET 1;
❖ EXPLANATION:
1. We group by Product_Name to get the total
quantity sold and total revenue generated for each
product.
2. SUM(o.Quantity_ordered_new) calculates the
total units sold per product.
3. SUM(o.Sales) calculates the total revenue per
product.
4. Ordering by Total_Quantity in descending order
and using OFFSET 1 allows us to select the second
most sold product.
➢Task 3:
Who are our Top 3 customers that return the
most Products?
❖ CODE:
SELECT o.Customer_Name, COUNT(r.Order_ID)
AS Return_Count
FROM Orders o
JOIN Returns r ON o.Order_ID = r.Order_ID
GROUP BY o.Customer_Name
ORDER BY Return_Count DESC
LIMIT 3;
❖ EXPLANATION:
1. We join the Orders and Returns tables on
Order_ID to identify returned orders.
2. Grouping by Customer_Name and counting
Order_ID in the Returns table gives us the return
count per customer.
3. We then order the results by Return_Count in
descending order and limit it to the top 3 customers.
➢Task 4:
Give me a month-wise breakdown of the
number of Products sold and the sales
generated through it.
❖ CODE:
SELECT DATE_FORMAT(o.Order_Date,
'%Y-%m') AS Month,
SUM(o.Quantity_ordered_new) AS
Products_Sold,
SUM(o.Sales) AS Total_Sales
FROM Orders o
GROUP BY Month
ORDER BY Month;
❖ EXPLANATION:
1. DATE_FORMAT(o.Order_Date, '%Y-%m')
extracts the year and month from the order date for
monthly grouping.
2. We calculate Products_Sold as the sum of
Quantity_ordered_new and Total_Sales as the sum
of Sales for each month.
3. The results are grouped by Month and ordered
chronologically.
➢Task 5:
Are there any other interesting insights we can
get from this data?
Possible insights might include:
Identifying the most profitable product
categories.
Examining return rates per region or manager.
Analyzing sales trends for specific customer
segments.
Example SQL Code for Product Category Analysis:
SELECT o.Product_Category, SUM(o.Sales) AS Total_Sales,
COUNT(r.Order_ID) AS Return_Count
FROM Orders o
LEFT JOIN Returns r ON o.Order_ID = r.Order_ID
GROUP BY o.Product_Category
ORDER BY Total_Sales DESC;
❖ EXPLANATION:
This query would show the total sales and the number of
returns per product category. By using a LEFT JOIN, it
includes products with zero returns. This insight could help
understand the categories with high revenue and low return
rates.