└── Data-Mining └── retail.sql /Data-Mining/retail.sql: -------------------------------------------------------------------------------- 1 | create schema retailshop; 2 | use retailshop; 3 | select * from online_retail; 4 | 5 | SELECT customerID, 6 | SUM(Quantity * UnitPrice) AS TotalSpace 7 | FROM online_retail 8 | GROUP BY CustomerID 9 | limit 10; 10 | 11 | SELECT CustomerID,count(DISTINCT StockCode) AS UniqueProduct 12 | FROM online_retail 13 | Group by CustomerID 14 | 15 | SELECT CustomerID,count(distinct InvoiceNo) AS NumberOfPurchase 16 | FROM online_retail 17 | group by CustomerID 18 | having count(distinct InvoiceNo) = 1; 19 | 20 | SELECT A.StockCode AS Product1, 21 | B.StockCode AS Product2, 22 | count(*) AS TimePurchasedTogether 23 | FROM online_retail AS A 24 | join online_retail B ON A.InvoiceNo = B.InvoiceNO AND A.StockCode> B.StockCode 25 | group by A.StockCode, B. StockCode 26 | order by TimePurchasedTogether desc; 27 | 28 | #AdvanceQueries 29 | select CustomerID, 30 | count(InvoiceNO) AS PurchaseFrequency 31 | from online_retail 32 | group by CustomerID 33 | order by PurchaseFrequency desc; 34 | 35 | select Country, 36 | avg(UnitPrice * Quantity) AS AverageOrderValue 37 | from online_retail 38 | group by Country 39 | order by AverageOrderValue desc; 40 | 41 | select CustomerID, 42 | max(InvoiceDate) AS LastPurchaseDate 43 | from online_retail 44 | group by CustomerID 45 | having max(InvoiceDate) < date_sub(curdate(), interval 6 month); 46 | 47 | SELECT A.StockCode AS Product1, 48 | B.StockCode AS Product2, 49 | count(*) AS TimePurchasedTogether 50 | FROM online_retail AS A 51 | join online_retail B ON A.InvoiceNo = B.InvoiceNO AND A.StockCode < B.StockCode 52 | group by A.StockCode, B. StockCode 53 | order by TimePurchasedTogether desc; 54 | 55 | SELECT 56 | YEAR(InvoiceDate) AS Year, 57 | MONTH(InvoiceDate) AS Month, 58 | SUM(UnitPrice * Quantity) AS TotalSales 59 | FROM 60 | online_retail 61 | GROUP BY 62 | Year, Month 63 | ORDER BY 64 | Year, Month; 65 | 66 | 67 | --------------------------------------------------------------------------------