Repository navigation
Expand file tree
/
Copy pathSQL Practice.sql
More file actions
583 lines (533 loc) · 12 KB
/
Copy pathSQL Practice.sql
File metadata and controls
583 lines (533 loc) · 12 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
Use Northwind_SPP
Go
--Question 1
--Return all the fields from all shippers
Select * from Shippers;
--Question 2
--Return two columns Category Name and Description
Select
CategoryName,
Description
from Categories;
--Question 3
--First, Last, HireDate of Sales Representatives
Select
FirstName,
LastName,
HireDate
from Employees
where
title = 'Sales Representative';
--Question 4
--First, Last, HireDate of U.S Sales Representatives3
Select
FirstName,
LastName,
HireDate
from Employees
where
title = 'Sales Representative'
and
Country = 'USA';
--Question 5
--Orders placed by a specific employee (Steven Buchanan - 5)
Select
OrderID,
OrderDate
from Orders
where EmployeeID = 5;
--Question 6
--Show suppliers whose contact title is NOT Marketing Manager
Select
SupplierID,
ContactName,
ContactTitle
from Suppliers
where ContactTitle != 'Marketing Manager';
--Question 7
--Find product with string "queso"
Select
ProductID,
ProductName
from Products
where ProductName like '%queso%';
--Question 8
--Select OrderID, CustomerID, ShipCountry in France/Belgium
Select
OrderID,
CustomerID,
ShipCountry
from Orders
where ShipCountry in ('France','Belgium');
--Question 9
--Select OrderID, CustomerID, ShipCountry in Latin Country
Select
OrderID,
CustomerID,
ShipCountry
from Orders
where
ShipCountry in ('Brazil',
'Mexico',
'Argentina',
'Venezuela');
--Question 10
--Employees, in order of age
Select
FirstName,
LastName,
Title,
BirthDate
from Employees
order by BirthDate asc;
--Question 11
--Employees, in order of age, date portion only
Select
FirstName,
LastName,
Title,
DateOnlyBirthDate = convert(date,BirthDate)
from Employees
order by BirthDate asc;
--Question 12
--Employee First, Last, and FullName
Select
FirstName,
LastName,
FullName = CONCAT(FirstName,' ',LastName)
from Employees;
--Question 13
--OrderDetails amount per line item
Select
OrderID,
ProductID,
UnitPrice,
Quantity,
TotalPrice = UnitPrice * Quantity
from OrderDetails;
--Question 14
--How many Customers
Select
count(distinct(CustomerID)) TotalCustomers
from Customers;
--Question 15
--First order ever made on Orders Table
Select
min(OrderDate) FirstOrder
from Orders;
--Question 16
--Show a list of countries where Northwind has customers
Select
distinct(Country)
from Customers
order by Country asc;
--Question 17
--Show how many contact titles for customers
Select
ContactTitle,
count(ContactTitle) TotalContactTitle
from Customers
group by ContactTitle
order by TotalContactTitle desc;
--Question 18
--Products with associated supplier name
Select
distinct ProductID,
ProductName,
CompanyName
from Products p
join Suppliers s
on p.SupplierID = s.SupplierID;
--Question 19
--OrderID, OrderDate, Shipper for previous orders
Select
OrderID,
OrderDate,
s.CompanyName
from Orders o
join Shippers s
on o.ShipVia = s.ShipperID
where OrderID < 10270;
--Question 20
--Calculate how many products per Category
Select
CategoryName,
count(p.ProductID) TotalProducts
from Categories c
join Products p
on c.CategoryID = p.CategoryID
group by CategoryName
order by TotalProducts desc;
--Question 21
--Total Customers per country/city
Select
Country,
City,
count(*) TotalCustomers
from Customers
group by
Country,
City
order by
TotalCustomers desc;
--Question 22
--Products that needs reordering
Select
ProductID,
ProductName,
UnitsInStock,
ReorderLevel
from Products
where UnitsInStock <= ReorderLevel
order by ProductID asc;
--Question 23
--Products that needs reordering and false discontinued flag
Select
ProductID,
ProductName,
UnitsInStock,
UnitsOnOrder,
ReorderLevel
from Products
where
UnitsOnOrder + UnitsInStock <= ReorderLevel
and
Discontinued = 0
order by ProductID asc;
--Question 24
--Country list by region, null region at the back of the table
Select
CustomerID,
CompanyName,
Region
from Customers
order by
(case when Region is NULL then 1 else 0 end),
Region,
CustomerID;
--Question 25
--Calculate top 3 average freight
Select
top 3
ShipCountry,
avg(Freight) AverageFreight
from Orders
group by ShipCountry
order by AverageFreight desc;
--Question 26 & 27
--High freight charges in 2015
Select
top 3
ShipCountry,
avg(Freight) AverageFreight
from Orders
where year(OrderDate) = 2015
group by ShipCountry
order by AverageFreight desc;
--Question 28
--High freight charges last year
Select
top 3
ShipCountry,
avg(Freight) AverageFreight
from Orders
where
OrderDate >= DATEADD(yy,-1,
(Select max(OrderDate) from Orders))
group by ShipCountry
order by AverageFreight desc;
--Question 29
--Employee/Order Detail report
Select
o.EmployeeID,
e.LastName,
o.OrderID,
p.ProductName,
Quantity
from Orders o
join Employees e
on o.EmployeeID = e.EmployeeID
join OrderDetails od
on o.OrderID = od.OrderID
join Products p
on od.ProductID = p.ProductID
order by
o.OrderID,
p.ProductName;
--Question 30
--Customers with no orders
Select
c.CustomerID [Customers_CustomerID],
o.CustomerID [Orders_CustomerID]
from Customers c
left join Orders o
on c.CustomerID = o.CustomerID
where o.CustomerID is NULL;
--Question 31
--Customers with no orders with EmployeeID 4
Select
c.CustomerID,
o.CustomerID
from Customers c
left join Orders o
on c.CustomerID = o.CustomerID
and o.EmployeeID = 4
where o.CustomerID is NULL;
--Question 32
--High value customers
Select
o.CustomerID,
CompanyName,
o.OrderID,
sum(UnitPrice * Quantity) TotalOrderAmount
from Orders o
join OrderDetails od
on o.OrderID = od.OrderID
join Customers c
on o.CustomerID = c.CustomerID
where year(OrderDate) = 2016
group by
o.CustomerID,
CompanyName,
o.OrderID
having sum(UnitPrice * Quantity) >= 10000
order by TotalOrderAmount desc;
--Question 33
--High value customers with total orders
Select
o.CustomerID,
CompanyName,
sum(UnitPrice * Quantity) TotalOrderAmount
from Orders o
join OrderDetails od
on o.OrderID = od.OrderID
join Customers c
on o.CustomerID = c.CustomerID
where year(OrderDate) = 2016
group by
o.CustomerID,
CompanyName
having sum(UnitPrice * Quantity) >= 15000
order by TotalOrderAmount desc;
--Question 34
--High-value customers with discount
Select
o.CustomerID,
CompanyName,
TotalWithoutDiscount = sum(UnitPrice * Quantity),
TotalWithDiscount = sum(UnitPrice * Quantity *(1-Discount))
from Orders o
join OrderDetails od
on o.OrderID = od.OrderID
join Customers c
on o.CustomerID = c.CustomerID
where year(OrderDate) = 2016
group by
o.CustomerID,
CompanyName
having sum(UnitPrice * Quantity) >= 15000
order by TotalWithDiscount desc;
--Question 35
--Month-end orders
Select
EmployeeID,
OrderID,
OrderDate
from Orders
where OrderDate = EOMONTH(OrderDate)
order by EmployeeID;
--Question 36
--Orders with many line items
Select
OrderID,
TotalOrderDetails = count(ProductID)
from OrderDetails
group by OrderID
order by TotalOrderDetails desc;
--Question 37
--Randomly select 2% OrderIDs
Select
top 2 percent OrderID
from Orders
order by NewID();
--Question 38 & 39 & 40
--Orders double-entry details (Quantity <60, double-entried ProductIDs)
With potentialdup as
(Select
OrderID
from OrderDetails
where Quantity >= 60
group by
OrderID,
Quantity
having count(*) > 1)
Select * from OrderDetails
where OrderID in (Select OrderID from potentialdup)
order by OrderID, Quantity;
--Question 41
--Find late orders
Select
OrderID,
OrderDate,
RequiredDate,
ShippedDate
from Orders
where ShippedDate >= RequiredDate;
--Question 42
--Find how many late orders for employees
Select
e.EmployeeID,
LastName,
TotalLateOrder = count(o.OrderID)
from Orders o
join Employees e
on o.EmployeeID = e.EmployeeID
where ShippedDate >= RequiredDate
group by e.EmployeeID, LastName
order by TotalLateOrder desc;
--Question 42-47
--Find how many all and late orders for employees
With
totalallorders as
(Select EmployeeID, allorders = count(*)
from Orders
group by EmployeeID),
totallateorders as
(Select EmployeeID, lateorders = count(*)
from Orders
where ShippedDate >= RequiredDate
group by EmployeeID)
Select
a.EmployeeID,
LastName,
isnull(lateorders,0) [Total Later Orders],
allorders [Total Orders],
[Percent Late Order] = convert(decimal(2,2),(isnull(lateorders,0)*1.00)/allorders)
from totalallorders a
full outer join totallateorders l
on a.EmployeeID = l.EmployeeID
join Employees e
on a.EmployeeID = e.EmployeeID
order by a.EmployeeID;
--Question 48 - 49
--Grouping Customers by Total Order Amount
With all2016orders as(
Select
o.CustomerID,
CompanyName,
TotalOrderAmount = sum(UnitPrice * Quantity)
from Orders o
join OrderDetails od
on o.OrderID = od.OrderID
join Customers c
on o.CustomerID = c.CustomerID
where year(OrderDate) = 2016
group by
o.CustomerID,
CompanyName)
Select
CustomerID,
CompanyName,
TotalOrderAmount,
CustomerGroup = case
when TotalOrderAmount >= 0 and TotalOrderAmount < 1000 then 'Low'
when TotalOrderAmount >= 1000 and TotalOrderAmount < 5000 then 'Medium'
when TotalOrderAmount >= 5000 and TotalOrderAmount < 10000 then 'High'
else 'Very High' end
from all2016orders
order by CustomerID;
--Question 50-51
--Customer grouping with percentage
With
all2016orders as(
Select
o.CustomerID,
CompanyName,
TotalOrderAmount = sum(UnitPrice * Quantity)
from Orders o
join OrderDetails od
on o.OrderID = od.OrderID
join Customers c
on o.CustomerID = c.CustomerID
where year(OrderDate) = 2016
group by
o.CustomerID,
CompanyName),
customergrouping as(
Select
CustomerID,
CompanyName,
TotalOrderAmount,
CustomerGroup = case
when TotalOrderAmount >= 0 and TotalOrderAmount < 1000 then 'Low'
when TotalOrderAmount >= 1000 and TotalOrderAmount < 5000 then 'Medium'
when TotalOrderAmount >= 5000 and TotalOrderAmount < 10000 then 'High'
else 'Very High' end
from all2016orders)
Select
CustomerGroup,
count(*) TotalInGroup,
PercentageInGroup = count(*)*1.0/(select count(*) from customergrouping)
from customergrouping
group by CustomerGroup
order by TotalInGroup desc;
--Question 52
--Country with Suppliers or Customers
Select distinct Country from Customers
union
Select distinct Country from Suppliers;
--Question 53
--Country with Suppliers or Customers compare 2 lists
With
countrycustomer as (Select distinct Country from Customers),
countrysupplier as (Select distinct Country from Suppliers)
Select
cs.Country SupplierCountry,
cc.Country CustomerCountry
from countrycustomer cc
full outer join countrysupplier cs
on cs.Country = cc.Country
--Question 54
--Country with Suppliers or Customers compare number of countries
With
countrycustomer as (Select Country, Total = count(*) from Customers group by Country),
countrysupplier as (Select Country, Total = count(*) from Suppliers group by Country)
Select
Country = isnull(cs.Country, cc.Country),
TotalSuppliers = isnull(cs.Total,0),
TotalCustomers = isnull(cc.Total,0)
from countrysupplier cs
full outer join countrycustomer cc
on cs.Country = cc.Country;
--Question 55
--First order in each country
With rankOrders as
(Select
ShipCountry,
CustomerID,
OrderID,
OrderDate,
OrderbyCountry = rank() over (partition by ShipCountry order by OrderDate)
from Orders)
Select ShipCountry,
CustomerID,
OrderID,
OrderDate
from rankOrders
where OrderbyCountry = 1;
--Question 56-57
--Customers with multiple orders in 5 day period
With NextOrderDate as
(Select
CustomerID,
OrderDate = convert(date,OrderDate),
NextOrderDate = convert(date,Lead(OrderDate,1)
over (partition by CustomerID order by CustomerID, OrderDate))
from Orders)
Select
CustomerID,
OrderDate,
NextOrderDate,
DaysBetweenOrders = Datediff(dd,OrderDate,NextOrderDate)
from NextOrderDate
where Datediff(dd,OrderDate,NextOrderDate) <= 5;