-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_Aggregations.sql
More file actions
260 lines (179 loc) · 7.43 KB
/
02_Aggregations.sql
File metadata and controls
260 lines (179 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- 2. Aggreagtions in SQL
-- Use Nrothwind DB
USE NorthWind_Commerce
-- Q1: Count the total number of products.
SELECT COUNT(*) AS [Total Number of Products]
FROM Products
-- alternatively, use count ProductName
SELECT COUNT(ProductName) AS [Total Number of Products]
FROM Products
-- 2. Find the total number of orders.
SELECT COUNT(*) AS TotalNumberofOrders
FROM Orders
-- 3. Find the average unit price of products.
SELECT AVG(UnitPrice) AS AverageUnitPrice
FROM Products
-- 4. Find the maximum unit price of products.
SELECT MAX(UnitPrice) AS MaxUnitPrice
FROM Products
-- 5. Find the minimum unit price of products.
SELECT MIN(UnitPrice) AS MinUnitPrice
FROM Products
-- 6. Find the total number of products per category.
SELECT CategoryID, COUNT(ProductName) AS ProductCount
FROM Products
GROUP BY CategoryID
-- alternatively, If you want to include Category Name, Join two table
SELECT C.CategoryName, COUNT(P.ProductName)
FROM Products AS P
JOIN Categories AS C ON C.CategoryID = P.CategoryID
GROUP BY C.CategoryName
-- 7. Find the average price of products per supplier.
SELECT SupplierID, AVG(UnitPrice) AS AvgUnitPrice
FROM Products
GROUP BY SupplierID
-- alternatively, Group By SupplierName and ProductName
SELECT SupplierID, ProductName, AVG(UnitPrice) AS AvgUnitPrice
FROM Products
GROUP BY SupplierID, ProductName
ORDER BY SupplierID, ProductName
-- 8. Find the total number of orders placed per customer.
SELECT CustomerID, COUNT(CustomerID) AS OrdersPerCustomer
FROM Orders
GROUP BY CustomerID
-- 9. Find the total revenue generated per product [Table: Order Details].
SELECT ProductID, SUM(UnitPrice) AS RevenuePerProduct
FROM [Order Details]
GROUP BY ProductID
-- alternatevely, add ProductName through JOIN
SELECT P.ProductName, O.ProductID, SUM(O.UnitPrice) AS RevenuePerProduct
FROM [Order Details] AS O
JOIN Products AS P ON P.ProductID = O.ProductID
GROUP BY P.ProductName, O.ProductID
-- 10. Find the maximum quantity ordered per order.
SELECT OrderID, MAX(Quantity)AS MaxQuantityPerOrder
FROM [Order Details]
GROUP BY OrderID
-- 11. Find categories with more than 5 products.
SELECT CategoryID, COUNT(ProductName) AS ProductCount
FROM Products
GROUP BY CategoryID
HAVING COUNT(ProductName) > 5
-- alternatovely, include CategoryName
SELECT C.CategoryName, P.CategoryID, COUNT(P.ProductName) AS ProductCount
FROM Categories AS C
JOIN Products AS P ON P.CategoryID = C.CategoryID
GROUP BY C.CategoryName, P.CategoryID
HAVING COUNT(P.ProductName) > 5
-- 12. Find suppliers with an average product price above 50.
SELECT S.CompanyName, P.SupplierID, AVG(UnitPrice) as AvgProductPrice
FROM Products AS P
JOIN Suppliers AS S ON S.SupplierID = P.SupplierID
GROUP BY S.CompanyName, P.SupplierID
HAVING AVG(UnitPrice) > 50
-- alteratively, if you do not require Supplier's CompanyName, you can avoid JOIN
-- 13. Find customers who have placed more than 10 orders.
SELECT CustomerID, COUNT(*) AS OrdersPerCustomer
FROM Orders
GROUP BY CustomerID
HAVING COUNT(CustomerID) > 10
-- 14. Find products (ProductName) with total sales greater than $10,000.
SELECT P.ProductName, O.ProductID, SUM(O.UnitPrice * O.Quantity) as TotalSales
FROM [Order Details] AS O
JOIN Products AS P ON P.ProductID = O.ProductID
GROUP BY P.ProductName, O.ProductID
HAVING SUM(O.UnitPrice * O.Quantity) > 10000
-- 15. Find employees with more than 60 orders handled.
SELECT Employees.FirstName,
Employees.LastName,
Orders.EmployeeID,
COUNT(Orders.EmployeeID) AS OrdersHandled
FROM Employees
JOIN Orders ON Orders.EmployeeID = Employees.EmployeeID
GROUP BY Employees.FirstName, Employees.LastName, Orders.EmployeeID
HAVING COUNT(Orders.EmployeeID) > 60
-- 16. Find the total number of products and their average price.
SELECT COUNT(*) AS TotalProducts, AVG(UnitPrice) AS AvgProductPrice
FROM Products
-- 17. Find the total orders and average freight per customer.
SELECT COUNT(*) as TotalOrders, AVG(Freight) as AvgFreightPerCustomer
FROM Orders
-- 18. Find the total revenue, minimum, and maximum sales per order.
SELECT SUM(UnitPrice * Quantity) AS TotalRevanue,
MIN(UnitPrice * Quantity) AS MinSalesPerOrder,
MAX(UnitPrice * Quantity) AS MaxSalesPerOrder
FROM [Order Details]
-- 19. Count the number of unique customers.
SELECT COUNT(DISTINCT(CustomerID)) AS UniqueCustomers
FROM Orders
-- 20. Count the number of unique products sold.
SELECT COUNT(DISTINCT(ProductID)) as UniqueProducts
FROM [Order Details]
-- 21. Find the total freight for orders shipped to Germany.
SELECT SUM(Freight) as TotalFreightCostGermany
FROM Orders
WHERE ShipCountry = 'Germany'
-- 22. Find the average order value for orders placed in 1997.
SELECT AVG(OS.UnitPrice * OS.Quantity) AS AvgOrderValue
FROM Orders as O
JOIN [Order Details] as OS on OS.OrderID = O.OrderID
WHERE YEAR(O.OrderDate) = '1997'
-- 23. Find the total revenue for products priced above 20.
SELECT SUM(UnitPrice * Quantity) as TotalRevenue
FROM [Order Details]
WHERE UnitPrice > 20
-- 24. Find the total revenue generated per category.
SELECT C.CategoryName, SUM(OD.UnitPrice * OD.Quantity) AS TotalRevenue
FROM [Order Details] AS OD
JOIN Products AS P ON P.ProductID = OD.ProductID
JOIN Categories AS C ON C.CategoryID = P.CategoryID
GROUP BY C.CategoryName
-- 25. Find the total orders placed by each customer in Germany.
SELECT C.CustomerID, COUNT(O.CustomerID) AS OrderCount
FROM Orders AS O
JOIN Customers AS C ON C.CustomerID = O.CustomerID
WHERE C.Country = 'Germany'
GROUP BY C.CustomerID
-- 26. Find the top 3 most frequently ordered products.
SELECT TOP 3 ProductID, (COUNT(ProductID)) AS Top3Products
FROM [Order Details]
GROUP BY ProductID
ORDER BY Top3Products DESC
-- additionally, fetch ProductName from Products
SELECT TOP 3 OD.ProductID, P.ProductName, (COUNT(OD.ProductID)) AS Top3Products
FROM [Order Details] AS OD
JOIN Products AS P ON P.ProductID = OD.ProductID
GROUP BY OD.ProductID, P.ProductName
ORDER BY Top3Products DESC
-- 27. Find the month with the highest number of orders.
SELECT TOP 1 MONTH(OrderDate) AS [Month], COUNT(*) AS MonthlyOrders
FROM Orders
GROUP BY MONTH(OrderDate)
ORDER BY MonthlyOrders DESC
-- 28. Calculate the percentage of total sales per product.
SELECT ProductID,
SUM(UnitPrice * Quantity) AS ProductSales,
SUM(UnitPrice * Quantity) * 100 / (SELECT SUM(UnitPrice * Quantity) as TotalSales FROM [Order Details]) AS SalesPercentage
FROM [Order Details]
GROUP BY ProductID
ORDER BY SalesPercentage DESC
-- additionally, add ProductName from Products
SELECT OD.ProductID,
P.ProductName,
SUM(OD.UnitPrice * OD.Quantity) AS ProductSales,
SUM(OD.UnitPrice * OD.Quantity) * 100 / (SELECT SUM(UnitPrice * Quantity) FROM [Order Details]) AS SalesPercentage
FROM [Order Details] AS OD
JOIN Products AS P ON P.ProductID = OD.ProductID
GROUP BY OD.ProductID, P.ProductName
ORDER BY SalesPercentage DESC
-- 29. Find products priced above the average price.
SELECT ProductName, ProductID, UnitPrice
FROM Products
WHERE UnitPrice > (SELECT AVG(UnitPrice) FROM Products)
-- 30. Find customers with orders totaling above $5,000.
SELECT CustomerID, SUM(UnitPrice * Quantity) AS TotalSpent
FROM [Order Details] OD
JOIN Orders O ON OD.OrderID = O.OrderID
GROUP BY CustomerID
HAVING SUM(UnitPrice * Quantity) > 5000
-- HAPPY PRACTICING!!