0% found this document useful (0 votes)
90 views15 pages

SQL Case Study

Uploaded by

ninawesanket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views15 pages

SQL Case Study

Uploaded by

ninawesanket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Case Study: Wide World Importers

Problem Statement:
As a data analyst, you have been tasked with analyzing the sales and customer data of
Worldwide Importers, a global company that specializes in importing and distributing various
products. Your goal is to extract meaningful insights from the data set using SQL queries. Along
with questions we have also given table hints below with every question.

Note: Kindly go through the below attached link for getting detail information on WideWorldImporters

Link: WideWorldImporters

Details are available in the link below.

https://dataedo.com/samples/html/WideWorldImporters/doc/WideWorldImporters_5/home.html

USE WideWorldImporters;

--1. How many orders have been placed on 26th June 2015?
--[sales].[Orders]

SELECT COUNT(*) AS Order_Count --[COUNT(*) is used to count all rows including NULL values]
FROM [sales].[Orders]
WHERE OrderDate = '2015-06-26';

--2. What is the total transaction amount done by the client whose CustomerID is 1?
--[Sales].[CustomerTransactions]

SELECT SUM(TransactionAmount) AS Total_Amount


FROM [Sales].[CustomerTransactions]
WHERE CustomerID = 1;

--3. How many unique secondary postal address lines are there?
--[Sales].[Customers]

SELECT COUNT(DISTINCT CONCAT(PostalAddressLine1, ' ', PostalAddressLine2)) AS


Unique_Secondary_Address_Lines
FROM [Sales].[Customers];
--4. What is the avg taxAmount given by each customer?
--[Sales].[CustomerTransactions]

SELECT AVG(TaxAmount) AS AverageTaxAmount


FROM [Sales].[CustomerTransactions];

--5. Display all the details of the stockItemID "10", last edited from 1st January 2013 to 31st December
2013.
--[Sales].[InvoiceLines]

SELECT *
FROM [Sales].[InvoiceLines]
WHERE StockItemID = 10
AND lastEditedWhen >= '2013-01-01'
AND lastEditedWhen <= '2013-12-31';

--6. Display the average unit price of all the stockitemID where average line profit exceeds more than
150.
--[Sales].[InvoiceLines]

SELECT StockItemID, AVG(unitPrice) AS Average_Unit_Price


FROM [Sales].[InvoiceLines]
GROUP BY StockItemID
HAVING AVG(LineProfit) > 150;

--7. Provide the complete address to the client by integrating the line 1, line 2 of DeliveryAddress and
PostalAddress.
--[Sales].[Customers]

SELECT CONCAT(DeliveryAddressLine1, ', ', DeliveryAddressLine2, ', ', PostalAddressLine1, ', ',
PostalAddressLine2)
AS Complete_Address
FROM [Sales].[Customers];
--8. Fetch all the records for the stock item name starting with USB.
--[Warehouse].[StockItems]

SELECT *
FROM [Warehouse].[StockItems]
WHERE StockItemName LIKE 'USB%';

--9. Calculate the Sales (product of quantity and unit price) sold when picking complete is 1st January
2013.
--[Sales].[OrderLines]

SELECT SUM(Quantity * UnitPrice) AS Total_Sales


FROM [Sales].[OrderLines]
WHERE PickingCompletedWhen >= '2013-01-01'
AND PickingCompletedWhen < '2013-01-02';

--10. Round off the transaction amount to the nearest integer value.
--[Sales].[CustomerTransactions]

SELECT ROUND(TransactionAmount, 0) AS Rounded_Amount


FROM [Sales].[CustomerTransactions];

--11. Who are the top 10 customers with the highest total purchase amount?
--[Sales].[Customers], [Sales].[Orders], [Sales].[OrderLines]

SELECT TOP 10 c.CustomerID, c.CustomerName, SUM(ol.Quantity * ol.UnitPrice) AS


Total_Purchase_Amount
FROM [Sales].[Customers] AS c
JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID
JOIN [Sales].[OrderLines] AS ol ON o.OrderID = ol.OrderID
GROUP BY c.CustomerID, c.CustomerName
ORDER BY Total_Purchase_Amount DESC;
--12. What is the total transaction amount for each year?
--[Sales].[CustomerTransactions]

SELECT YEAR(TransactionDate) AS Transaction_Year, SUM(TransactionAmount) AS Total_Amount


FROM [Sales].[CustomerTransactions]
GROUP BY YEAR(TransactionDate);

--13. How many customers have made repeat purchases?


--[Sales].[Orders]

SELECT COUNT(*) AS Total_Repeat_Customers


FROM (
SELECT CustomerID
FROM [Sales].[Orders]
GROUP BY CustomerID
HAVING COUNT(*) > 1) AS Repeat_Customers;

--14. How many stock items in the warehouse have a quantity on hand below the reorder level?
--[Warehouse].[StockItemHoldings]

SELECT COUNT(*) AS Low_Stock_Items


FROM [Warehouse].[StockItemHoldings]
WHERE QuantityOnHand < ReorderLevel;

--15. What is the total value of stock items held in the warehouse?
--[Warehouse].[StockItemHoldings],[Warehouse].[StockItems]

SELECT SUM(si.UnitPrice * sih.QuantityOnHand) AS Total_Stock_Value


FROM [Warehouse].[StockItems] AS si
JOIN [Warehouse].[StockItemHoldings] AS sih ON si.StockItemID = sih.StockItemID;
--16. What is the average quantity on hand for each stock item name in the warehouse?
--[Warehouse].[StockItemHoldings],[Warehouse].[StockItems]

SELECT si.StockItemName, AVG(sih.QuantityOnHand) AS Average_Quantity_On_Hand


FROM [Warehouse].[StockItems] AS si
JOIN [Warehouse].[StockItemHoldings] AS sih ON si.StockItemID = sih.StockItemID
GROUP BY si.StockItemName;

--17. Which stock item has the highest quantity on hand in the warehouse?
--[Warehouse].[StockItemHoldings],[Warehouse].[StockItems]

SELECT TOP 1 si.StockItemName, sih.QuantityOnHand


FROM [Warehouse].[StockItems] AS si
JOIN [Warehouse].[StockItemHoldings] AS sih ON si.StockItemID = sih.StockItemID
ORDER BY sih.QuantityOnHand DESC;

--18. What is the rank of each customer transaction based on the Amount transacted for each
Transaction Type?
--[Sales].[CustomerTransactions],[Application].[TransactionTypes]

SELECT c.CustomerID, t.TransactionTypeID, t.TransactionAmount,


RANK() OVER (PARTITION BY t.TransactionTypeID ORDER BY t.TransactionAmount DESC) AS
Transaction_Rank
FROM [Sales].[CustomerTransactions] AS t
JOIN [Application].[TransactionTypes] AS tt ON t.TransactionTypeID = tt.TransactionTypeID
JOIN [Sales].[Customers] AS c ON t.CustomerID = c.CustomerID;

--19. Retrieve the total sales revenue for each year and month, as well as the grand total for all years
and months.
--[Sales].[Orders],[Sales].[OrderLines]

SELECT YEAR(o.OrderDate) AS Order_Year, MONTH(o.OrderDate) AS Order_Month,


SUM(ol.Quantity * ol.UnitPrice) AS Total_Sales_Revenue
FROM [Sales].[Orders] AS o
JOIN [Sales].[OrderLines] AS ol ON o.OrderID = ol.OrderID
GROUP BY YEAR(o.OrderDate), MONTH(o.OrderDate)
WITH ROLLUP; --[WITH ROLLUP modifier is used to include an additional row at the end of the result
set, providing the grand total for all years and months.]
--20. Display the detail of the orders received by the customers on Each Date.
--[Sales].[Invoices],[Sales].[Customers]

SELECT inv.InvoiceDate, c.CustomerID, c.CustomerName, inv.DeliveryInstructions AS


Complete_Address
FROM [Sales].[Invoices] AS inv
JOIN [Sales].[Customers] AS c ON inv.CustomerID = c.CustomerID
ORDER BY inv.InvoiceDate;

--21. Display the order details (order ID, customer name, order date) for all orders placed in the year
2015 and in the Month of May.
--[Sales].[Orders], [Sales].[Customers]

SELECT o.OrderID, c.CustomerName, o.OrderDate


FROM [Sales].[Orders] AS o
JOIN [Sales].[Customers] AS c ON o.CustomerID = c.CustomerID
WHERE YEAR(o.OrderDate) = 2015 AND MONTH(o.OrderDate) = 5;

--22. Start a transaction, update the amount of a supplier transaction where SupplierTransactionID is
12345, and rollback the transaction.
--[Purchasing].[SupplierTransactions]

BEGIN TRANSACTION;

UPDATE [Purchasing].[SupplierTransactions]
SET AmountExcludingTax = 56789 --[56789 demo amount]
WHERE SupplierTransactionID = 12345;

ROLLBACK TRANSACTION;

--23. Retrieve the unique list of product names from the "Sales.OrderLines" and
"Purchasing.SupplierTransactions" tables.
--[Sales].[OrderLines], [Purchasing].[SupplierTransactions]

--24. Retrieve the unique customer IDs between the "Sales.Customers" and "Sales.OrderLines" tables.
--[Sales].[Customers], [Sales].[Orders]

SELECT DISTINCT c.CustomerID


FROM [Sales].[Customers] AS c
JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID;
--25. Retrieve the common customer IDs between the "Sales.Customers" and "Sales.OrderLines"
tables.
--[Sales].[Customers], [Sales].[Orders]

SELECT c.CustomerID
FROM [Sales].[Customers] AS c
INNER JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID;

--26. Retrieve all products that are currently out of stock.


--[Warehouse].[StockItems]

SELECT *
FROM [Warehouse].[StockItems]
WHERE QuantityPerOuter = 0;

--27. Retrieve the total number of orders placed by each customer.


--[Sales].[Customers], [Sales].[Orders]

SELECT c.CustomerID, c.CustomerName, COUNT(o.OrderID) AS TotalOrders


FROM [Sales].[Customers] AS c
LEFT JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c.CustomerName;

--28. Retrieve the top 10 products with the highest unit price.
--[Warehouse].[StockItems]

SELECT TOP 10 StockItemID, StockItemName, UnitPrice


FROM [Warehouse].[StockItems]
ORDER BY UnitPrice DESC;

--29. Retrieve the order details for a specific order with the given OrderID.
--[Sales].[OrderLines], [Warehouse].[StockItems]

SELECT ol.OrderLineID, ol.OrderID, ol.StockItemID, si.StockItemName, ol.Quantity, ol.UnitPrice,


ol.TaxRate, ol.PickedQuantity
FROM [Sales].[OrderLines] AS ol
JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
WHERE ol.OrderID = 45; --[45 is demo OrderID]
--30. Retrieve all products that have the word "blue" in their name.
--[Warehouse].[StockItems]

SELECT *
FROM [Warehouse].[StockItems]
WHERE StockItemName LIKE '%blue%';

--31. Retrieve the total number of orders placed each month.


--[Sales].[Orders]

SELECT YEAR(OrderDate) AS OrderYear, MONTH(OrderDate) AS OrderMonth, COUNT(OrderID) AS


TotalOrders
FROM [Sales].[Orders]
GROUP BY YEAR(OrderDate), MONTH(OrderDate)
ORDER BY OrderYear, OrderMonth;

--32. Retrieve the products with a unit price higher than the average unit price.
--[Warehouse].[StockItems]

SELECT *
FROM [Warehouse].[StockItems]
WHERE UnitPrice > (SELECT AVG(UnitPrice) FROM [Warehouse].[StockItems]);

--33. Retrieve the top 5 products with the highest sales quantity.
--[Sales].[OrderLines], [Warehouse].[StockItems]

SELECT TOP 5 sl.StockItemID, si.StockItemName, SUM(sl.Quantity) AS TotalQuantity


FROM [Sales].[OrderLines] AS sl
JOIN [Warehouse].[StockItems] AS si ON sl.StockItemID = si.StockItemID
GROUP BY sl.StockItemID, si.StockItemName
ORDER BY TotalQuantity DESC;

--34. Retrieve the customers who have not placed any orders.
--[Sales].[Customers],[Sales].[Orders]

SELECT *
FROM [Sales].[Customers] AS c
LEFT JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID
WHERE o.CustomerID IS NULL;
--35. Retrieve the customers who have placed orders in the last 10 years.
--[Sales].[Customers],[Sales].[Orders]

SELECT c.*
FROM [Sales].[Customers] AS c
WHERE EXISTS (
SELECT 1
FROM [Sales].[Orders] AS o
WHERE c.CustomerID = o.CustomerID
AND o.OrderDate >= DATEADD(YEAR, -10, GETDATE())
);

--36. How many customers are general retailers?


--[Sales].[Customers], [Sales].[CustomerCategories]

SELECT COUNT(*) AS RetailerCount


FROM [Sales].[Customers] AS c
INNER JOIN [Sales].[CustomerCategories] AS cc ON c.CustomerCategoryID = cc.CustomerCategoryID
WHERE cc.CustomerCategoryName = 'General Retailer';

--37. Find the cities with no information on the population.


--[Application].[Cities]

SELECT DISTINCT CityName


FROM [Application].[Cities]
WHERE LatestRecordedPopulation IS NULL
ORDER BY CityName ASC;

--38. Calculate the percentage of customers held by each type of customer category in the total
customer pool?
--[Sales].[Customers], [Sales].[CustomerCategories]

SELECT cc.CustomerCategoryName, COUNT(c.CustomerID) AS CustomerCount,


COUNT(c.CustomerID) * 100.0 / (SELECT COUNT(*) FROM [Sales].[Customers]) AS Percentage
FROM [Sales].[Customers] AS c
INNER JOIN [Sales].[CustomerCategories] AS cc ON c.CustomerCategoryID = cc.CustomerCategoryID
GROUP BY cc.CustomerCategoryName;
--39. List all the employee details of Wide World Importers who are not permitted to login to the system.
--[Application].[People]

SELECT *
FROM [Application].[People]
WHERE IsPermittedToLogon = 0;

--40. Find details of the top 5 highest purchasing customers in 2014.


--[Sales].[CustomerTransactions], [Sales].[OrderLines]

--41. Find the details of the top 5 sales persons with highest orders initiated.
--[Sales].[Orders], [Sales].[OrderLines]

--42. List the top 5 highest purchasing cities.


--[Application].[Cities], [Sales].[OrderLines]

--43. Which is the largest order ever placed? Display its details.
--[Sales].[OrderLines]

SELECT TOP 1 OrderID, SUM(Quantity * UnitPrice) AS TotalAmount


FROM [Sales].[OrderLines]
GROUP BY OrderID
ORDER BY TotalAmount DESC;

--44. Find the total revenue produced by each supermarket customer in 2013.
--[Sales].[OrderLines], [Sales].[Customers]

SELECT c.CustomerID, c.CustomerName, SUM(ol.Quantity * ol.UnitPrice) AS TotalRevenue


FROM [Sales].[OrderLines] AS ol
JOIN [Sales].[Orders] AS o ON ol.OrderID = o.OrderID
JOIN [Sales].[Customers] AS c ON o.CustomerID = c.CustomerID
WHERE YEAR(o.OrderDate) = 2013
GROUP BY c.CustomerID, c.CustomerName;
--45. Retrieve the order details for orders placed in the year 2016.
--[Sales].[Orders], [Sales].[OrderLines], [Warehouse].[StockItems]

SELECT o.OrderID, o.OrderDate, ol.StockItemID, si.StockItemName, ol.Quantity, ol.UnitPrice


FROM [Sales].[Orders] AS o
JOIN [Sales].[OrderLines] AS ol ON o.OrderID = ol.OrderID
JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
WHERE YEAR(o.OrderDate) = 2016;

--46. Retrieve the top 10 products with the highest profit margin.
--[Warehouse].[StockItems]

SELECT TOP 10 StockItemName, (UnitPrice - RecommendedRetailPrice) AS ProfitMargin


FROM [Warehouse].[StockItems]
ORDER BY ProfitMargin DESC;

--47. Retrieve the average unit price for each supplier.


--[Purchasing].[Suppliers],[Warehouse].[StockItems]

SELECT s.SupplierName, AVG(si.UnitPrice) AS AverageUnitPrice


FROM [Purchasing].[Suppliers] AS s
JOIN [Warehouse].[StockItems] AS si ON s.SupplierID = si.SupplierID
GROUP BY s.SupplierName;

--48. Calculate the total revenue generated by each product category.


--[Sales].[Orders], [Warehouse].[StockItems] , [Warehouse].[StockItemStockGroups],
[Warehouse].[StockGroups]

SELECT sg.StockGroupName, SUM(ol.Quantity * ol.UnitPrice) AS TotalRevenue


FROM [Sales].[Orders] AS o
JOIN [Sales].[OrderLines] AS ol ON o.OrderID = ol.OrderID
JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
JOIN [Warehouse].[StockItemStockGroups] AS ssg ON si.StockItemID = ssg.StockItemID
JOIN [Warehouse].[StockGroups] AS sg ON ssg.StockGroupID = sg.StockGroupID
GROUP BY sg.StockGroupName;
--49. Retrieve the names of all products with a unit price greater than $50.
--[Warehouse].[StockItems]

SELECT StockItemName
FROM [Warehouse].[StockItems]
WHERE UnitPrice > 50;

--50. Find the total quantity sold for each product.


--[Sales].[OrderLines], [Warehouse].[StockItems]

SELECT si.StockItemName, SUM(ol.Quantity) AS TotalQuantitySold


FROM [Sales].[OrderLines] AS ol
JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
GROUP BY si.StockItemName;

--51. Calculate the average duration of delivery spent on Every Delivery Method to deliver to Customer.
--[Sales].[Invoices], [Application].[DeliveryMethods], [Sales].[Orders]

SELECT dm.DeliveryMethodName, AVG(DATEDIFF(DAY, o.OrderDate, i.ConfirmedDeliveryTime)) AS


AverageDeliveryDuration
FROM [Sales].[Invoices] AS i
JOIN [Sales].[Orders] AS o ON i.OrderID = o.OrderID
JOIN [Application].[DeliveryMethods] AS dm ON i.DeliveryMethodID = dm.DeliveryMethodID
GROUP BY dm.DeliveryMethodName;

--52. Retrieve the names of customers who have placed more than 100 orders.
--[Sales].[Customers], [Sales].[Orders].

SELECT c.CustomerName
FROM [Sales].[Customers] AS c
INNER JOIN (
SELECT CustomerID, COUNT(*) AS OrderCount
FROM [Sales].[Orders]
GROUP BY CustomerID
HAVING COUNT(*) > 100
) AS o ON c.CustomerID = o.CustomerID;
--53. Display the names of customers who have placed at least one order in each month of 2013.
--[Sales].[Customers], [Sales].[Orders]

SELECT c.CustomerName
FROM [Sales].[Customers] AS c
INNER JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID
WHERE YEAR(o.OrderDate) = 2013
GROUP BY c.CustomerName
HAVING COUNT(DISTINCT MONTH(o.OrderDate)) = 12;

--54. Calculate the total revenue for each year.


--[Sales].[OrderLines],[Sales].[Orders]

SELECT YEAR(o.OrderDate) AS OrderYear, SUM(ol.Quantity * ol.UnitPrice) AS TotalRevenue


FROM [Sales].[OrderLines] AS ol
INNER JOIN [Sales].[Orders] AS o ON ol.OrderID = o.OrderID
GROUP BY YEAR(o.OrderDate)
ORDER BY OrderYear;

--55. Retrieve the names of customers who have placed orders for products with a unit price between
$50 and $100.
--[Sales].[Customers], [Sales].[Orders], [Sales].[OrderLines], [Warehouse].[StockItems]

SELECT DISTINCT c.CustomerName


FROM [Sales].[Customers] AS c
INNER JOIN [Sales].[Orders] AS o ON c.CustomerID = o.CustomerID
INNER JOIN [Sales].[OrderLines] AS ol ON o.OrderID = ol.OrderID
INNER JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
WHERE si.UnitPrice BETWEEN 50 AND 100;

--56. What are the top 5 best-selling products in terms of revenue?


--[Sales].[OrderLines], [Warehouse].[StockItems]

SELECT TOP 5 si.StockItemName, SUM(ol.Quantity * ol.UnitPrice) AS Revenue


FROM [Sales].[OrderLines] AS ol
INNER JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
GROUP BY si.StockItemName
ORDER BY Revenue DESC;
--57. How many new customers were acquired each month?
--[Sales].[Customers], [Sales].[Orders]

SELECT YEAR(o.OrderDate) AS Year, MONTH(o.OrderDate) AS Month, COUNT(DISTINCT


c.CustomerID) AS NewCustomers
FROM [Sales].[Orders] AS o
INNER JOIN [Sales].[Customers] AS c ON o.CustomerID = c.CustomerID
GROUP BY YEAR(o.OrderDate), MONTH(o.OrderDate)
ORDER BY Year, Month;

--58. How many orders have been placed in each quarter of the year?
--[Sales].[Orders]

SELECT YEAR(OrderDate) AS Year,


CASE
WHEN MONTH(OrderDate) BETWEEN 1 AND 3 THEN 'Q1'
WHEN MONTH(OrderDate) BETWEEN 4 AND 6 THEN 'Q2'
WHEN MONTH(OrderDate) BETWEEN 7 AND 9 THEN 'Q3'
WHEN MONTH(OrderDate) BETWEEN 10 AND 12 THEN 'Q4'
END AS Quarter,
COUNT(*) AS NumberOfOrders
FROM [Sales].[Orders]
GROUP BY YEAR(OrderDate),
CASE
WHEN MONTH(OrderDate) BETWEEN 1 AND 3 THEN 'Q1'
WHEN MONTH(OrderDate) BETWEEN 4 AND 6 THEN 'Q2'
WHEN MONTH(OrderDate) BETWEEN 7 AND 9 THEN 'Q3'
WHEN MONTH(OrderDate) BETWEEN 10 AND 12 THEN 'Q4'
END
ORDER BY Year, Quarter;

--59. How many new customers were acquired each year?


--[Sales].[Customers], [Sales].[Orders]

SELECT YEAR(o.OrderDate) AS Year, COUNT(DISTINCT c.CustomerID) AS NewCustomers


FROM [Sales].[Orders] AS o
INNER JOIN [Sales].[Customers] AS c ON o.CustomerID = c.CustomerID
GROUP BY YEAR(o.OrderDate)
ORDER BY Year;
--60. Retrieve the average quantity sold per month for each product.
--[Sales].[OrderLines], [Sales].[Orders], [Warehouse].[StockItems]

SELECT YEAR(o.OrderDate) AS Year, MONTH(o.OrderDate) AS Month, si.StockItemName,


AVG(ol.Quantity) AS AverageQuantitySold
FROM [Sales].[OrderLines] AS ol
INNER JOIN [Sales].[Orders] AS o ON ol.OrderID = o.OrderID
INNER JOIN [Warehouse].[StockItems] AS si ON ol.StockItemID = si.StockItemID
GROUP BY YEAR(o.OrderDate), MONTH(o.OrderDate), si.StockItemName
ORDER BY Year, Month, si.StockItemName;

You might also like