-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChapter_14.sql
More file actions
605 lines (604 loc) · 15 KB
/
Chapter_14.sql
File metadata and controls
605 lines (604 loc) · 15 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**
* @Author: ADITYA KUMAR SINGH <the__martian>
* @Email: cr7.aditya.cs@gmail.com
* @Filename: Chapter_14.sql
* @Last modified by: the__martian
*/
-----------------------------------------------------------------
I N T E R N A L P R O B L E M S
-----------------------------------------------------------------
use EntertainmentAgencyExample;
-----------------------------------------------------------------
/*
“Show me the entertainer groups that play in a jazz style
and have more than three members.”
*/
select
E.EntStageName,
count(*)
from
Entertainers E
inner join
Entertainer_Styles ES
on E.EntertainerID = ES.EntertainerID
inner join
Musical_Styles MS
on MS.StyleID = ES.StyleID
inner join
Entertainer_Members EM
on EM.EntertainerID = E.EntertainerID
where
MS.StyleName = 'Jazz'
group by
E.EntStageName
having
count(EM.MemberID) > 3;
-----------------------------------------------------------------
use SalesOrdersExample;
-----------------------------------------------------------------
/*
“Show me the states on the west coast of the United States where
the total of the orders is greater than $1 million.”
*/
select
C.CustState,
sum(OD.QuotedPrice * OD.QuantityOrdered) as TotalOrders
from
Customers C
inner join
Orders O
on O.CustomerID = C.CustomerID
inner join
Order_Details OD
on OD.OrderNumber = O.OrderNumber
where
C.CustState in ('WA', 'OR', 'CA')
group by
C.CustState
having
sum(OD.QuotedPrice * OD.QuantityOrdered) > 1000000
-----------------------------------------------------------------
use SchoolSchedulingExample;
-----------------------------------------------------------------
/*
“Show me the subject categories that have fewer than three
full professors teaching that subject.”
*/
select
C.CategoryDescription,
count(FC.StaffID) as ProfCount
from
Categories C
inner join
Faculty_Categories FC
on C.CategoryID = FC.CategoryID
inner join
Faculty F
on F.StaffID = FC.StaffID
where
F.Title = "Professor"
group by
C.CategoryDescription
Having
count(FC.StaffID) < 3;
/*
Above query will not show result with o ProCount.
Following query will contain missing rows
*/
select
C.CategoryDescription,
(
select
count(f.StaffID)
from
Faculty f
inner join
Faculty_Categories fc
on f.StaffID = fc.StaffID
inner join
Categories as c
on c.CategoryID = fc.CategoryID
where
c.CategoryID = C.CategoryID
and
f.Title = 'Professor'
) as ProCount
from
Categories C
where
(
select
count(F.StaffID)
from
Faculty F
inner join
Faculty_Categories FC
on F.StaffID = FC.StaffID
inner join
Categories Cat
on Cat.CategoryID = FC.CategoryID
where
Cat.CategoryID = C.CategoryID
and
F.Title = 'Professor'
) < 3;
-----------------------------------------------------------------
P R A C T I C E P R O B L E M
-----------------------------------------------------------------
use SalesOrdersExample;
-----------------------------------------------------------------
/*
“List for each customer and order date the customer’s full name
and the total cost of items ordered that is greater than $1,000.”
*/
select
concat(C.CustFirstName, ' ', C.CustLastName) as FullName,
O.OrderDate,
sum(OD.QuotedPrice * OD.QuantityOrdered) as TotalCost
from
Customers C
inner join
Orders O
on O.CustomerID = C.CustomerID
inner join
Order_Details OD
on OD.OrderNumber = O.OrderNumber
group by
C.CustFirstName,
C.CustLastName,
O.OrderDate
having
sum(OD.QuotedPrice * OD.QuantityOrdered) > 1000;
-----------------------------------------------------------------
use EntertainmentAgencyExample;
-----------------------------------------------------------------
/*
“Which agents booked more than $3,000 worth of business in
December 2017?”
*/
select
concat(A.AgtFirstName, ' ', A.AgtLastName) as FullName,
sum(E.ContractPrice) as BookedPrice
from
Agents A
inner join
Engagements E
on A.AgentID = E.AgentID
where
E.StartDate between '2017-12-01' and '2017-12-31'
group by
A.AgtFirstName,
A.AgtLastName
having
sum(E.ContractPrice) > 3000;
7
use SchoolSchedulingExample;
-----------------------------------------------------------------
/*
“For completed classes, list by category and student the category
name, the student name, and the student’s average grade of all
classes taken in that category for those students who have an
average higher than 90.”
*/
select
C.CategoryDescription,
concat(S.StudFirstName, ' ', S.StudLastName) as Student,
avg(SS.Grade) as AvgGrade
from
Students S
inner join
Student_Schedules SS
on S.StudentID = SS.StudentID
inner join
Student_Class_Status SCS
on SCS.ClassStatus = SS.ClassStatus
inner join
Classes CL
on CL.ClassID = SS.ClassID
inner join
Subjects SB
on SB.SubjectID = CL.SubjectID
inner join
Categories C
on C.CategoryID = SB.CategoryID
where
SCS.ClassStatusDescription = "Completed"
group by
C.CategoryDescription,
S.StudFirstName,
S.StudLastName
having
avg(SS.Grade) > 90;
-----------------------------------------------------------------
/*
“List each staff member and the count of classes each is scheduled
to teach for those staff members who teach at least one but fewer
than three classes.”
*/
select
concat(S.StfFirstName, ' ', S.StfLastname) as Staff,
count(FC.ClassID) as NumberOfClass
from
Staff S
inner join
Faculty_Classes FC
on S.StaffID = FC.StaffID
group by
S.StfFirstName,
S.StfLastname
having
count(FC.ClassID) < 3;
-----------------------------------------------------------------
use BowlingLeagueExample;
-----------------------------------------------------------------
/*
“List the bowlers whose highest raw scores are more than 20 pins
higher than their current averages.”
*/
select
concat(B.BowlerFirstName, ' ', B.BowlerLastName) as Bowler,
round(avg(BS.RawScore), 0) as CurrentAverage,
max(BS.RawScore) as MaxRowScore
from
Bowlers B
inner join
Bowler_Scores BS
on B.BowlerID = BS.BowlerID
group by
B.BowlerFirstName,
B.BowlerLastName
having
max(BS.RawScore) > (round(avg(BS.RawScore), 0) + 20)
order by
B.BowlerFirstName,
B.BowlerLastName;
-----------------------------------------------------------------
use RecipesExample;
-----------------------------------------------------------------
/*
“List the recipes that contain both beef and garlic.”
*/
select
R.RecipeTitle
from
Recipes R
where
R.RecipeID in (
select
RI.RecipeID
from
Recipe_Ingredients RI
inner join
Ingredients I
on I.IngredientID = RI.IngredientID
where
I.IngredientName = 'Beef'
)
and
R.RecipeID in (
select
RI.RecipeID
from
Recipe_Ingredients RI
inner join
Ingredients I
on I.IngredientID = RI.IngredientID
where
I.IngredientName = 'Garlic'
);
-----------------------------------------------------------------
EXCERCISE QUESTION
-----------------------------------------------------------------
use SalesOrdersExample;
-----------------------------------------------------------------
/*
“Show me each vendor and the average by vendor of the number of
days to deliver products that are greater than the average
delivery days for all vendors.”
*/
select
V.VendName,
avg(PV.DaysToDeliver) as AvgDayToDeliverProducts
from
Vendors V
inner join
Product_Vendors PV
on V.VendorID = PV.VendorID
group by
V.VendName
having
avg(PV.DaysToDeliver) > (
select
avg(pv.DaysToDeliver)
from
Product_Vendors pv
);
-----------------------------------------------------------------
/*
“Display for each product the product name and the total sales
that is greater than the average of sales for all products in
that category.”
*/
????
-----------------------------------------------------------------
/*
“How many orders are for only one product?”
*/
select
count(res.OrderNumber)
from
(
select
OD.OrderNumber
from
Order_Details OD
group by
OD.OrderNumber
having
count(*) = 1;
) as res;
-----------------------------------------------------------------
use EntertainmentAgencyExample;
-----------------------------------------------------------------
/*
“Show me the entertainers who have more than two overlapped
bookings.”
*/
select
Ent.EntertainerID,
Ent.EntStageName
from
Entertainers Ent
where
Ent.EntertainerID in
(
select
E1.EntertainerID
from
Engagements E1
inner join
Engagements E2
on E1.EntertainerID = E2.EntertainerID
where
E1.EngagementNumber <> E2.EngagementNumber
and
E1.StartDate <= E2.EndDate
and
E2.Startdate <= E1.EndDate
group by
E1.EntertainerID
order by
count(*) >= 2
);
-----------------------------------------------------------------
/*
“Show each agent’s name, the sum of the contract price for the
engagements booked, and the agent’s total commission for agents
whose total commission is more than $1,000.”
*/
select
concat(A.AgtFirstName, ' ', A.AgtLastName) as Agent,
sum(E.ContractPrice) as TotalContractPrice,
sum(E.ContractPrice * A.CommissionRate) as TotalCommission
from
Agents A
inner join
Engagements E
on A.AgentID = E.AgentID
group by
A.AgtFirstName,
A.AgtLastName
having
sum(E.ContractPrice * A.CommissionRate) > 1000;
-----------------------------------------------------------------
use SchoolSchedulingExample;
-----------------------------------------------------------------
/*
“Display by category the category name and the count of classes
offered for those categories that have three or more classes.”
*/
select
C.CategoryDescription,
count(*) TotalClasses
from
Categories C
inner join
Subjects S
on C.CategoryID = S.CategoryID
inner join
Classes CL
on CL.SubjectID = S.SubjectID
group by
C.CategoryDescription
having
count(*) >= 3;
-----------------------------------------------------------------
/*
“List each staff member and the count of classes each is
scheduled to teach for those staff members who teach fewer than
three classes.”
*/
select
concat(S.StfFirstName, ' ', S.StfLastname) as Staff,
count(FC.ClassID) TotalClasses
from
Staff S
left join
Faculty_Classes as FC
on FC.StaffID = S.StaffID
group by
S.StfFirstName,
S.StfLastname
having
count(FC.ClassID) < 3;
-----------------------------------------------------------------
/*
“Show me the subject categories that have fewer than three full
professors teaching that subject.”
*/
select
C.CategoryDescription,
count(FCF.StaffID) as ProfCount
from
Categories C
left join
(
select
FC.CategoryID,
FC.StaffID
from
Faculty_Categories FC
inner join
Faculty F
on F.StaffID = FC.StaffID
where
F.Title = "Professor"
) as FCF
on FCF.CategoryID = C.CategoryID
group by
C.CategoryDescription
having
count(FCF.StaffID) < 3;
-----------------------------------------------------------------
/*
“Count the classes taught by every staff member.”
*/
select
concat(S.stfFirstName, ' ', S.stfLastName) as Staff,
count(FC.ClassID) as ClassCount
from
Staff S
left join
Faculty_Classes FC
on S.StaffID = FC.StaffID
group by
S.stfFirstName,
S.StfLastname;
-----------------------------------------------------------------
use BowlingLeagueExample;
-----------------------------------------------------------------
/*
“Do any team captains have a raw score that is higher than any
other member of the team?”
*/
select
T.TeamID,
B.BowlerID,
concat(B.BowlerFirstName, ' ', B.BowlerLastName) as Captain,
max(BS.RawScore) as HighestRawScore
from
Bowlers B
inner join
Teams T
on B.BowlerID = T.CaptainID
inner join
Bowler_Scores BS
on BS.BowlerID = B.BowlerID
group by
T.TeamID,
B.BowlerID,
B.BowlerFirstName,
B.BowlerLastName
having
max(BS.RawScore) > (
select
max(bs.RawScore)
from
Bowler_Scores bs
inner join
Bowlers b
on b.BowlerID = bs.BowlerID
where
bs.BowlerID <> B.BowlerID
and
b.TeamID = T.TeamID
);
-----------------------------------------------------------------
/*
“Display for each bowler the bowler name and the average of the
bowler’s raw game scores for bowlers whose average is greater
than 155.”
*/
select
concat(B.BowlerFirstName, ' ', B.BowlerLastName) as Bowler,
avg(BS.RawScore) as AvgRawScore
from
Bowlers B
inner join
Bowler_Scores BS
on BS.BowlerID = B.BowlerID
group by
B.BowlerFirstName,
B.BowlerLastName
having
avg(BS.RawScore) > 155;
-----------------------------------------------------------------
/*
“List the last name and first name of every bowler whose average
raw score is greater than or equal to the overall average score.”
*/
select
B.BowlerLastName,
B.BowlerFirstName,
avg(BS.RawScore) as AvgRawScore
from
Bowlers B
inner join
Bowler_Scores BS
on BS.BowlerID = B.BowlerID
group by
B.BowlerFirstName,
B.BowlerLastName
having
avg(BS.RawScore) >= (
select
avg(bs.RawScore)
from
Bowler_Scores bs
);
-----------------------------------------------------------------
use RecipesExample;
-----------------------------------------------------------------
/*
“Sum the amount of salt by recipe class, and display those
recipe classes that require more than three teaspoons.”
*/
select
RC.RecipeClassDescription,
sum(RI.Amount)
from
Recipe_Classes RC
inner join
Recipes R
on RC.RecipeClassID = R.RecipeClassID
inner join
Recipe_Ingredients as RI
on RI.RecipeID = R.RecipeID
inner join
Ingredients I
on I.IngredientID = RI.IngredientID
where
I.IngredientName = 'Salt'
group by
RC.RecipeClassDescription
having
sum(RI.Amount) > 3;
-----------------------------------------------------------------
/*
“For what class of recipe do I have two or more recipes?”
*/
select
RC.RecipeClassDescription,
count(R.RecipeID)
from
Recipe_Classes RC
inner join
Recipes R
on RC.RecipeClassID = R.RecipeClassID
group by
RC.RecipeClassDescription
having
count(R.RecipeID) >= 2;
-----------------------------------------------------------------
T H E - E N D
-----------------------------------------------------------------