0% found this document useful (0 votes)
878 views

NorthWind DB Query Case Study Solution

The document contains 15 SQL queries that retrieve data from database tables to: 1) Show orders with customer details, products with categories, and available categories. 2) Calculate sales by category and product for 1997 and each category's sales. 3) Show yearly sales by category and product, and yearly category sales. 4) List customers, order subtotals, employee sales by country and year, freight charges, and full order details. 5) Define a view and query to review order summaries.

Uploaded by

saketsj
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)
878 views

NorthWind DB Query Case Study Solution

The document contains 15 SQL queries that retrieve data from database tables to: 1) Show orders with customer details, products with categories, and available categories. 2) Calculate sales by category and product for 1997 and each category's sales. 3) Show yearly sales by category and product, and yearly category sales. 4) List customers, order subtotals, employee sales by country and year, freight charges, and full order details. 5) Define a view and query to review order summaries.

Uploaded by

saketsj
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
You are on page 1/ 7

Database Model

1. Want to see All Orders with Customer Detail

SELECT Orders.OrderID, Orders.OrderDate, Orders.CustomerID, Customers.CompanyName,


Customers.Address,

Customers.City, Customers.Region, Customers.PostalCode, Customers.Country, Customers.Phone

FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID

ORDER BY Orders.OrderID;

2. I need All Product with Category

SELECT Categories.CategoryName, Products.ProductName, Categories.Description,

Products.ProductID, Products.QuantityPerUnit, Products.UnitPrice, Products.Discontinued

FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID

WHERE Products.Discontinued = 0

ORDER BY Categories.CategoryName, Products.ProductName;

3. Which Category List we have for products

SELECT Categories.CategoryName, Categories.CategoryID FROM Categories ORDER BY


Categories.CategoryName;
4. What was Sales of Category By each Product for year 1997

SELECT Categories.CategoryName, Products.ProductName, SUM( (Order_Details.UnitPrice *


Order_Details.Quantity) * (1 - Order_Details.Discount) / 100) * 100

AS ProductSales

FROM Orders INNER JOIN

Order_Details ON Orders.OrderID = Order_Details.OrderID

INNER JOIN Products ON Products.ProductID = Order_Details.ProductID

INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID

WHERE (Orders.ShippedDate BETWEEN '1997-01-01 00:00:00' AND '1997-12-31 00:00:00')

GROUP BY Categories.CategoryName, Products.ProductName;

5. What was sales of each Category for year 1997

SELECT Categories.CategoryName, SUM( (Order_Details.UnitPrice * Order_Details.Quantity) *


(1 - Order_Details.Discount) / 100) * 100

AS ProductSales

FROM Orders INNER JOIN

Order_Details ON Orders.OrderID = Order_Details.OrderID

INNER JOIN Products ON Products.ProductID = Order_Details.ProductID

INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID

WHERE (Orders.ShippedDate BETWEEN '1997-01-01 00:00:00' AND '1997-12-31 00:00:00')


GROUP BY Categories.CategoryName;
6. What was Yearly sale of each Category Product vise

SELECT Year(Orders.OrderDate) AS Year,Categories.CategoryName,Products.ProductName,


SUM( (Order_Details.UnitPrice * Order_Details.Quantity) * (1 - Order_Details.Discount) / 100) *
100

AS ProductSales

FROM Orders INNER JOIN

Order_Details ON Orders.OrderID = Order_Details.OrderID

INNER JOIN Products ON Products.ProductID = Order_Details.ProductID

INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID

GROUP BY Year(Orders.OrderDate), Categories.CategoryName,Products.ProductName

7. What was Yearly Category Sales

SELECT Year(Orders.OrderDate) AS Year,Categories.CategoryName, SUM(


(Order_Details.UnitPrice * Order_Details.Quantity) * (1 - Order_Details.Discount) / 100) * 100

AS ProductSales

FROM Orders INNER JOIN

Order_Details ON Orders.OrderID = Order_Details.OrderID

INNER JOIN Products ON Products.ProductID = Order_Details.ProductID

INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID

GROUP BY Year(Orders.OrderDate), Categories.CategoryName


8. What is our list of Customer

SELECT Customers.CompanyName, Customers.CustomerID

FROM Customers

ORDER BY Customers.CompanyName;

9. Show The list of subtotal of each order

SELECT OrderID, SUM( (UnitPrice * Quantity) * (1 - Discount) / 100) * 100 AS Subtotal

FROM Order_Details

GROUP BY OrderID

10. List of Employee Sales by Country

SELECT LastName AS Salesperson, Employees.Country, SUM( (UnitPrice * Quantity) * (1 -


Discount) / 100) * 100 AS Sale_Amount
FROM
Employees INNER JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID
INNER JOIN Order_Details ON Orders.OrderID = Order_Details.OrderID

Group By LastName , Employees.Country;


11. List of Employee Sales for a single Year

SELECT LastName AS Salesperson, SUM( (UnitPrice * Quantity) * (1 - Discount) / 100) * 100


AS Sale_Amount
FROM
Employees INNER JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID
INNER JOIN Order_Details ON Orders.OrderID = Order_Details.OrderID
where (Orders.OrderDate BETWEEN '1997-01-01 00:00:00' AND '1997-12-31 00:00:00')
Group By LastName;

12. List of Sales of employee every year

SELECT YEAR(Orders.OrderDate) as 'Year',LastName AS Salesperson, SUM( (UnitPrice *


Quantity) * (1 - Discount) / 100) * 100 AS Sale_Amount
FROM
Employees INNER JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID
INNER JOIN Order_Details ON Orders.OrderID = Order_Details.OrderID
WHERE (Orders.OrderDate BETWEEN '1997-01-01 00:00:00' AND '1997-12-31 00:00:00')
Group By LastName ,YEAR(Orders.OrderDate);

13. What is list of Freight Charges for Every Shipping

SELECT Shippers.CompanyName, Orders.OrderID, Orders.Freight, Orders.ShippedDate


FROM Shippers INNER JOIN Orders ON Shippers.ShipperID = Orders.ShipVia

ORDER BY Shippers.CompanyName, Orders.OrderID;

14. List of Complete detail of every order

SELECT Order_Details.OrderID, Products.ProductName, Order_Details.ProductID,


Order_Details.UnitPrice, Order_Details.Quantity, Order_Details.Discount,
(Order_Details.UnitPrice*Quantity*(1-Discount)*100)/100 AS Price
FROM Products INNER JOIN Order_Details ON Products.ProductID =
Order_Details.ProductID;

15. Review Order Summary (Using View )

CREATE VIEW Order_Subtotals As


SELECT OrderID, SUM( (UnitPrice * Quantity) * (1 - Discount) / 100) * 100 AS Subtotal
FROM Order_Details
GROUP BY OrderID

SELECT Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate,


Orders.CustomerID, Customers.CompanyName, Employees.LastName, Shippers.CompanyName AS
ShipVia, Order_Subtotals.Subtotal, Orders.Freight, Subtotal+Freight AS Total
FROM
Employees INNER JOIN
Orders ON Employees.EmployeeID = Orders.EmployeeID INNER JOIN Order_Subtotals ON
Order_Subtotals.OrderID = Orders.OrderID
INNER JOIN Shippers ON Shippers.ShipperID = Orders.ShipVia Inner Join Customers ON
Customers.CustomerID = Orders.CustomerID
ORDER BY Orders.OrderID DESC;

You might also like