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

Assignment7 Answers

Uploaded by

142414003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment7 Answers

Uploaded by

142414003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Northwind Database SQL Queries

1. Product Names, Supplier Names, and Unit Prices

SELECT

p.ProductName,

s.SupplierName,

p.UnitPrice

FROM

Products p

JOIN

Suppliers s ON p.SupplierID = s.SupplierID;

2. Customers Who Placed Orders in 2023

SELECT

DISTINCT c.CustomerID,

o.OrderDate

FROM

Customers c

JOIN

Orders o ON c.CustomerID = o.CustomerID

WHERE

YEAR(o.OrderDate) = 2023;

3. Suppliers Providing Products in Each Category

SELECT

c.CategoryName,

s.SupplierName

FROM
Categories c

JOIN

Products p ON c.CategoryID = p.CategoryID

JOIN

Suppliers s ON p.SupplierID = s.SupplierID

GROUP BY

c.CategoryName, s.SupplierName;

4. Products Never Ordered

SELECT

p.ProductName

FROM

Products p

LEFT JOIN

OrderDetails od ON p.ProductID = od.ProductID

WHERE

od.OrderID IS NULL;

5. Customers with Purchases from >3 Categories and Avg Order Value > $500

SELECT

c.CustomerID

FROM

Customers c

JOIN

Orders o ON c.CustomerID = o.CustomerID

JOIN

OrderDetails od ON o.OrderID = od.OrderID


JOIN

Products p ON od.ProductID = p.ProductID

GROUP BY

c.CustomerID

HAVING

COUNT(DISTINCT p.CategoryID) > 3

AND AVG(od.UnitPrice * od.Quantity) > 500;

6. Number of Products Sold in Each Category

SELECT

c.CategoryName,

SUM(od.Quantity) AS TotalSold

FROM

Categories c

JOIN

Products p ON c.CategoryID = p.CategoryID

JOIN

OrderDetails od ON p.ProductID = od.ProductID

GROUP BY

c.CategoryName;

7. Customers Who Have Not Placed Orders

SELECT

c.CustomerID

FROM

Customers c

LEFT JOIN
Orders o ON c.CustomerID = o.CustomerID

WHERE

o.OrderID IS NULL;

For customers who have placed orders and their details

SELECT

c.CustomerID,

o.OrderID,

o.OrderDate,

o.ShippedDate

FROM

Customers c

JOIN

Orders o ON c.CustomerID = o.CustomerID;

8. Orders Shipped After Required Date

SELECT

o.OrderID,

DATEDIFF(o.ShippedDate, o.RequiredDate) AS DaysLate

FROM

Orders o

WHERE

o.ShippedDate > o.RequiredDate;

9. Suppliers with Most Products Sold

SELECT

s.SupplierName,
SUM(od.Quantity) AS TotalSold

FROM

Suppliers s

JOIN

Products p ON s.SupplierID = p.SupplierID

JOIN

OrderDetails od ON p.ProductID = od.ProductID

GROUP BY

s.SupplierName

ORDER BY

TotalSold DESC;

10. Products Sold by Each Supplier and Suppliers Not Sold Any Products

SELECT

s.SupplierName,

COUNT(od.ProductID) AS ProductsSold

FROM

Suppliers s

LEFT JOIN

Products p ON s.SupplierID = p.SupplierID

LEFT JOIN

OrderDetails od ON p.ProductID = od.ProductID

GROUP BY

s.SupplierName;

11. Customers Who Purchased Products from More than One Category

SELECT
c.CustomerID

FROM

Customers c

JOIN

Orders o ON c.CustomerID = o.CustomerID

JOIN

OrderDetails od ON o.OrderID = od.OrderID

JOIN

Products p ON od.ProductID = p.ProductID

GROUP BY

c.CustomerID

HAVING

COUNT(DISTINCT p.CategoryID) > 1;

12. Employees with >5 Orders but No 'Tofu'

SELECT

e.EmployeeID

FROM

Employees e

JOIN

Orders o ON e.EmployeeID = o.EmployeeID

LEFT JOIN

OrderDetails od ON o.OrderID = od.OrderID

LEFT JOIN

Products p ON od.ProductID = p.ProductID AND p.ProductName LIKE '%Tofu%'

GROUP BY

e.EmployeeID
HAVING

COUNT(o.OrderID) > 5

AND SUM(CASE WHEN p.ProductID IS NOT NULL THEN 1 ELSE 0 END) = 0;

13. Customers Ordered >5 Different Products in 2023

SELECT

c.CustomerID

FROM

Customers c

JOIN

Orders o ON c.CustomerID = o.CustomerID

JOIN

OrderDetails od ON o.OrderID = od.OrderID

WHERE

YEAR(o.OrderDate) = 2023

GROUP BY

c.CustomerID

HAVING

COUNT(DISTINCT od.ProductID) > 5;

14. Employees Processing Orders with Products from >3 Categories

SELECT

e.EmployeeID,

SUM(od.UnitPrice * od.Quantity) AS TotalRevenue

FROM

Employees e

JOIN
Orders o ON e.EmployeeID = o.EmployeeID

JOIN

OrderDetails od ON o.OrderID = od.OrderID

JOIN

Products p ON od.ProductID = p.ProductID

GROUP BY

e.EmployeeID

HAVING

COUNT(DISTINCT p.CategoryID) > 3;

You might also like