Final Project
Final Project
Final Project
(1, 1, 1, 1, 1000),
(2, 1, 2, 1, 500),
(3, 2, 3, 2, 300),
(4, 3, 4, 1, 100),
(5, 4, 5, 5, 20);
-- i. Retrieve customers who have placed orders for more than one product
category
SELECT DISTINCT Customer.CustomerID, Customer.FirstName, Customer.LastName
FROM Customer
JOIN [Order] ON Customer.CustomerID = [Order].CustomerID
JOIN OrderDetail ON [Order].OrderID = OrderDetail.OrderID
JOIN Product ON OrderDetail.ProductID = Product.ProductID
GROUP BY Customer.CustomerID, Customer.FirstName, Customer.LastName
HAVING COUNT(DISTINCT Product.Category) > 1;
-- j. Calculate the average order value for each customer and rank them by
their average order value
SELECT CustomerID, AVG(TotalAmount) AS AverageOrderValue,
RANK() OVER (ORDER BY AVG(TotalAmount) DESC) AS Rank
FROM [Order]
GROUP BY CustomerID;
-- k. Identify customers who have not made any purchases in the last 3 months
SELECT Customer.CustomerID, Customer.FirstName, Customer.LastName
FROM Customer
LEFT JOIN [Order] ON Customer.CustomerID = [Order].CustomerID
WHERE [Order].OrderDate IS NULL OR [Order].OrderDate < DATEADD(MONTH, -3,
GETDATE());
-- l. Retrieve the top 3 customers who have spent the most amount of money in
total
SELECT CustomerID, SUM(TotalAmount) AS TotalSpent
FROM [Order]
GROUP BY CustomerID
ORDER BY TotalSpent DESC
LIMIT 3;