Email : putriangelina865@gmail.com
Instagram : https://www.instagram.com/putriangelinaw
RPubs : https://rpubs.com/putriangelinaw/
SELECT CustomerID, CustomerName, CustomerOrder
FROM (SELECT c.*, COUNT(OD.Quantity) AS 'CustomerOrder'
FROM customers c
JOIN orders o
ON c.CustomerID = o.CustomerID
JOIN orderdetails od
ON o.OrderID = od.OrderID
GROUP BY od.OrderID) AS co
GROUP BY co.CustomerID
HAVING CustomerOrder > AVG(CustomerOrder);SELECT ProductID, ProductName, Country, Revenue
FROM (SELECT p.*, SUM(od.Quantity*p.Price) AS 'Revenue', Country
FROM products p
JOIN orderdetails od
ON p.ProductID = od.ProductID
JOIN orders o
ON od.OrderID=o.OrderID
JOIN customers c
ON o.CustomerID=c.CustomerID
GROUP BY p.ProductName, c.Country
ORDER BY Revenue DESC) AS r
GROUP BY Country
ORDER BY Revenue DESC;(SELECT ProductName, YEAR(o.OrderDate) Year, SUM(Quantity) Quantity
FROM products p
JOIN orderdetails od
ON od.ProductID=p.ProductID
JOIN orders o
ON o.OrderID=od.OrderID
GROUP BY Year, ProductName
HAVING Year=1996
ORDER BY Quantity DESC
LIMIT 10)
UNION
(SELECT ProductName, YEAR(o.OrderDate) Year, SUM(Quantity) Quantity
FROM products p
JOIN orderdetails od
ON od.ProductID=p.ProductID
JOIN orders o
ON o.OrderID=od.OrderID
GROUP BY Year, ProductName
HAVING Year=1997
ORDER BY Quantity DESC
LIMIT 10)