-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level a task
300 lines (206 loc) · 8.16 KB
/
Level a task
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
***Level A tasks****
List of all customers:
SELECT * FROM Customers;
List of all customers where company name ending in N:
SELECT * FROM Customers
WHERE CompanyName LIKE '%N';
List of all customers who live in Berlin or London:
SELECT * FROM Customers
WHERE City IN ('Berlin', 'London');
List of all customers who live in UK or USA:
SELECT * FROM Customers
WHERE Country IN ('UK', 'USA');
List of all products sorted by product name:
SELECT * FROM Products
ORDER BY ProductName;
List of all products where product name starts with an A:
SELECT * FROM Products
WHERE ProductName LIKE 'A%';
List of customers who ever placed an order:
SELECT DISTINCT Customers.*
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
List of Customers who live in London and have bought chai:
SELECT DISTINCT Customers.*
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE Customers.City = 'London' AND Products.ProductName = 'Chai';
List of customers who never placed an order:
SELECT * FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);
List of customers who ordered Tofu:
SELECT DISTINCT Customers.*
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE Products.ProductName = 'Tofu';
Details of first order of the system:
SELECT * FROM Orders
ORDER BY OrderDate
LIMIT 1;
Find the details of most expensive order date:
SELECT Orders.*, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS Total
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Orders.OrderID
ORDER BY Total DESC
LIMIT 1;
For each order get the OrderID and Average quantity of items in that order:
SELECT OrderID, AVG(Quantity) AS AvgQuantity
FROM OrderDetails
GROUP BY OrderID;
For each order get the OrderID, minimum quantity and maximum quantity for that order:
SELECT OrderID, MIN(Quantity) AS MinQuantity, MAX(Quantity) AS MaxQuantity
FROM OrderDetails
GROUP BY OrderID;
Get a list of all managers and total number of employees who report to them:
SELECT ManagerID, COUNT(EmployeeID) AS NumberOfEmployees
FROM Employees
GROUP BY ManagerID;
Get the OrderID and the total quantity for each order that has a total quantity of greater than 300:
SELECT OrderID, SUM(Quantity) AS TotalQuantity
FROM OrderDetails
GROUP BY OrderID
HAVING TotalQuantity > 300;
List of all orders placed on or after 1996/12/31:
SELECT * FROM Orders
WHERE OrderDate >= '1996-12-31';
List of all orders shipped to Canada:
SELECT * FROM Orders
WHERE ShipCountry = 'Canada';
List of all orders with order total > 200:
SELECT Orders.*, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS OrderTotal
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Orders.OrderID
HAVING OrderTotal > 200;
List of countries and sales made in each country:
SELECT ShipCountry, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalSales
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY ShipCountry;
List of Customer ContactName and number of orders they placed:
SELECT Customers.ContactName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.ContactName;
List of customer contact names who have placed more than 3 orders:
SELECT Customers.ContactName
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.ContactName
HAVING COUNT(Orders.OrderID) > 3;
List of discontinued products which were ordered between 1/1/1997 and 1/1/1998:
SELECT DISTINCT Products.*
FROM Products
JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
WHERE Products.Discontinued = 1
AND Orders.OrderDate BETWEEN '1997-01-01' AND '1998-01-01';
List of employee first name, last name, supervisor first name, last name:
SELECT e.FirstName, e.LastName, m.FirstName AS SupervisorFirstName, m.LastName AS SupervisorLastName
FROM Employees e
LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID;
List of employee IDs and total sales conducted by employee:
SELECT Orders.EmployeeID, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalSales
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Orders.EmployeeID;
List of employees whose FirstName contains character 'a':
SELECT * FROM Employees
WHERE FirstName LIKE '%a%';
List of managers who have more than four people reporting to them:
SELECT ManagerID
FROM Employees
GROUP BY ManagerID
HAVING COUNT(EmployeeID) > 4;
List of Orders and ProductNames:
SELECT Orders.OrderID, Products.ProductName
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;
List of orders placed by the best customer:
WITH BestCustomer AS (
SELECT CustomerID, COUNT(OrderID) AS NumberOfOrders
FROM Orders
GROUP BY CustomerID
ORDER BY NumberOfOrders DESC
LIMIT 1
)
SELECT Orders.*
FROM Orders
JOIN BestCustomer ON Orders.CustomerID = BestCustomer.CustomerID;
List of orders placed by customers who do not have a Fax number:
SELECT Orders.*
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
WHERE Customers.Fax IS NULL;
List of postal codes where the product Tofu was shipped:
SELECT DISTINCT Orders.ShipPostalCode
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE Products.ProductName = 'Tofu';
List of product names that were shipped to France:
SELECT DISTINCT Products.ProductName
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE Orders.ShipCountry = 'France';
List of ProductNames and Categories for the supplier 'Specialty Biscuits, Ltd.':
SELECT Products.ProductName, Categories.CategoryName
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN Categories ON Products.CategoryID = Categories.CategoryID
WHERE Suppliers.CompanyName = 'Specialty Biscuits, Ltd.';
List of products that were never ordered:
SELECT * FROM Products
WHERE ProductID NOT IN (SELECT ProductID FROM OrderDetails);
List of products where units in stock is less than 10 and units on order are 0:
SELECT * FROM Products
WHERE UnitsInStock < 10 AND UnitsOnOrder = 0;
List of top 10 countries by sales:
SELECT ShipCountry, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalSales
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY ShipCountry
ORDER BY TotalSales DESC
LIMIT 10;
Number of orders each employee has taken for customers with CustomerIDs between A and AO:
SELECT EmployeeID, COUNT(OrderID) AS NumberOfOrders
FROM Orders
WHERE CustomerID BETWEEN 'A' AND 'AO'
GROUP BY EmployeeID;
Order date of most expensive order:
SELECT OrderDate
FROM Orders
JOIN (
SELECT OrderID, SUM(Quantity * UnitPrice) AS Total
FROM OrderDetails
GROUP BY OrderID
ORDER BY Total DESC
LIMIT 1
) AS MostExpensiveOrder ON Orders.OrderID = MostExpensiveOrder.OrderID;
Product name and total revenue from that product:
SELECT Products.ProductName, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalRevenue
FROM OrderDetails
JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY Products.ProductName;
SupplierID and number of products offered:
SELECT SupplierID, COUNT(ProductID) AS NumberOfProducts
FROM Products
GROUP BY SupplierID;
Top ten customers based on their business:
SELECT Customers.CustomerID, Customers.ContactName, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalBusiness
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Customers.CustomerID, Customers.ContactName
ORDER BY TotalBusiness DESC
LIMIT 10;
What is the total revenue of the company:
SELECT SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalRevenue
FROM OrderDetails;