Dbms Exercise 2
Dbms Exercise 2
Write a query to display the CustomerName of customers who have purchased more than 5
different products.
SELECT c.CustomerName
FROM Customers c
WHERE c.CustomerID IN (
SELECT s.CustomerID
FROM Sales s
GROUP BY s.CustomerID
HAVING COUNT(DISTINCT s.ProductID) > 5
);
4. Find Products That Have Never Been Purchased
Write a query to display the names of products that have never been sold.
SELECT p.ProductName
FROM Products p
WHERE p.ProductID NOT IN (
SELECT s.ProductID
FROM Sales s
);
SELECT c.CustomerName, (
SELECT SUM(s.TotalAmount)
FROM Sales s
WHERE s.CustomerID = c.CustomerID
) AS TotalExpenditure
FROM Customers c;
6. Find the Customer Who Has Spent the Most
Write a query to find the CustomerName of the customer who has spent the most amount
of money in the shopping mart.
SELECT c.CustomerName
FROM Customers c
WHERE c.CustomerID = (
SELECT s.CustomerID
FROM Sales s
GROUP BY s.CustomerID
ORDER BY SUM(s.TotalAmount) DESC
LIMIT 1);