SQL Practice - 5.
Joins
Queries:
SELECT c.CustomerName, o.OrderID, o.OrderDate FROM Customers c JOIN Orders o ON
c.CustomerID = o.CustomerID;
SELECT c.CustomerName FROM Customers c LEFT JOIN Orders o ON c.CustomerID =
o.CustomerID WHERE o.OrderID IS NULL;
SELECT o.OrderID, p.ProductName, od.Quantity FROM Orders o JOIN OrderDetails od ON
o.OrderID = od.OrderID JOIN Products p ON od.ProductID = p.ProductID;
SELECT c.CustomerName, SUM(p.Price * od.Quantity) AS TotalSpent 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.CustomerName ORDER BY
TotalSpent DESC;
SELECT p.ProductName, SUM(od.Quantity) AS TotalOrdered FROM OrderDetails od JOIN
Products p ON od.ProductID = p.ProductID GROUP BY p.ProductName ORDER BY TotalOrdered
DESC LIMIT 5;