-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChapter_09.sql
419 lines (416 loc) · 10.9 KB
/
Chapter_09.sql
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
/**
* @Author: ADITYA KUMAR SINGH <the__martian>
* @Email: cr7.aditya.cs@gmail.com
* @Filename: Chapter_9.sql
* @Last modified by: the__martian
*/
------------------------------------------------------------------
I N T E R N A L P R O B L E M S
------------------------------------------------------------------
use SalesOrdersExample;
------------------------------------------------------------------
select distinct Products.ProductNumber, ProductName
from
Products
left join
Order_Details
on Products.ProductNumber = Order_Details.ProductNumber
where
Order_Details.OrderNumber is null;
------------------------------------------------------------------
select
concat(C.CustFirstName, ' ', C.CustLastName) as Customers,
bi.ProductName, bi.OrderDate, bi.QuotedPrice,
bi.QuantityOrdered
from
Customers C
left join
(
select
O.CustomerID, O.OrderDate,
OD.QuotedPrice, OD.QuantityOrdered, P.ProductName
from
Orders O
inner join
Order_Details OD
on OD.OrderNumber = O.OrderNumber
inner join
Products P
on P.ProductNumber = OD.ProductNumber
inner join
Categories Ca
on Ca.CategoryID = P.CategoryID
where
Ca.CategoryDescription = 'Bikes'
) as bi
on C.CustomerID = bi.CustomerID;
------------------------------------------------------------------
use EntertainmentAgencyExample;
------------------------------------------------------------------
select
EntStageName
from
Entertainers
left join
Engagements
on Entertainers.EntertainerID = Engagements.EntertainerID
where
Engagements.EntertainerID is NULL;
------------------------------------------------------------------
select
Ct.Name, Ms.StyleName
from
Musical_Styles as Ms
left outer join
(
select
concat(C.CustFirstName, ' ', C.CustLastName) as Name,
MP.StyleID
from
Customers as C
inner join
Musical_Preferences as MP
on MP.CustomerID = C.CustomerID
) as Ct
on Ms.StyleID = Ct.StyleID;
----------------------------------------------------------
use SchoolSchedulingExample;
----------------------------------------------------------
select
concat(S.StfFirstName, ' ', S.StfLastname) as Name
from
Staff S
left join
Faculty_Classes FC
on FC.StaffID = S.StaffID
where
FC.StaffID is null;
----------------------------------------------------------
select
concat(s.StudFirstName, ' ', s.StudLastName) as Name
from
Students as s
left join
(
select
ss.StudentID
from
Student_Schedules as ss
inner join
Student_Class_Status as scs
on ss.ClassStatus = scs.ClassStatus
where
scs.ClassStatusDescription = 'Withdrew'
) as wid
on wid.StudentID = s.StudentID
where
wid.StudentID is null;
----------------------------------------------------------
select
sc.CategoryDescription, sc.SubjectName,
Cl.ClassRoomID, Cl.StartDate, Cl.StartTime, Cl.Duration
from
(
select
C.CategoryDescription, S.SubjectID, S.SubjectName
from
Categories as C
left join
Subjects as S
on S.CategoryID = C.CategoryID
) as sc
left join
Classes as Cl
on Cl.SubjectID = sc.SubjectID;
----------------------------------------------------------
use BowlingLeagueExample;
----------------------------------------------------------
select
T.TourneyID, T.TourneyDate, T.TourneyLocation
from
Tournaments as T
left join
Tourney_Matches as TM
on T.TourneyID = TM.TourneyID
where
TM.TourneyID is null;
----------------------------------------------------------
select
concat(B.BowlerFirstName, " ", B.BowlerLastName) as Bowlers,
BB.TourneyLocation as 'Location', BB.TourneyDate as 'Date',
BB.MatchID as MatchID, BB.RawScore as "Raw Score"
from
Bowlers B
left join
(
select
BS.MatchID, BS.BowlerID, BS.RawScore,
T.TourneyDate, T.TourneyLocation
from
Bowler_Scores as BS
inner join
Tourney_Matches as TM
on TM.MatchID = BS.MatchID
inner join
Tournaments as T
on T.TourneyID = TM.TourneyID
where
BS.RawScore > 180
) as BB
on BB.BowlerID = B.BowlerID;
----------------------------------------------------------
use RecipesExample;
----------------------------------------------------------
select
I.IngredientName
from
Ingredients I
left join
Recipe_Ingredients RI
on I.IngredientID = RI.IngredientID
where
RI.IngredientID is null;
----------------------------------------------------------
???????
----------------------------------------------------------
--PROBLEMS
----------------------------------------------------------
use SalesOrdersExample;
----------------------------------------------------------
select
concat(C.CustFirstName, " ", C.CustLastName) as Customer
from
Customers C
left outer join
(
select O.CustomerID
from
Orders O
inner join
Order_Details OD
on OD.OrderNumber = O.OrderNumber
inner join
Products P
on P.ProductNumber = OD.ProductNumber
where
P.ProductName like "%Helmet%"
) OH
on OH.CustomerID = C.CustomerID
where
OH.CustomerID is null;
----------------------------------------------------------
----output 27 rows, but book say there is 18 rows.
select distinct
C.CustomerID, C.Customer, C.CustZipCode
from
(
select
concat(CC.CustFirstName, " ", CC.CustLastName) as Customer,
O.EmployeeID, CC.CustomerID, CC.CustZipCode
from
Customers CC
inner join
Orders O
on O.CustomerID = CC.CustomerID
) C
left join
Employees E
on E.EmployeeID = C.EmployeeID
where
E.EmpZipCode <> C.CustZipCode;
----------------------------------------------------------
-- OUTPUT 3973 rows, but books says 2681 rows
select
PO.ProductNumber, PO.ProductName, O.OrderDate
from
Orders O
left join
(
select P.ProductName, P.ProductNumber, OD.OrderNumber
from
Products P
inner join
Order_Details OD
on OD.ProductNumber = P.ProductNumber
) PO
on PO.OrderNumber = O.OrderNumber
where
PO.OrderNumber is not null
order by
PO.ProductNumber, PO.ProductName, O.OrderDate;
----------------------------------------------------------
use EntertainmentAgencyExample;
----------------------------------------------------------
select
A.AgtLastName, A.AgtFirstName
from
Agents A
left join
Engagements EA
on A.AgentID = EA.AgentID
where E
A.AgentID is null;
----------------------------------------------------------
select
C.CustLastName, C.CustFirstName
from
Customers C
left join
Engagements E
on E.CustomerID = C.CustomerID
where
E.CustomerID is null;
----------------------------------------------------------
select
EntStageName, Engagements.EngagementNumber
from
Entertainers
left join
Engagements
on Entertainers.EntertainerID = Engagements.EntertainerID;
----------------------------------------------------------
use SchoolSchedulingExample;
----------------------------------------------------------
select
SC.SubjectName, SC.ClassRoomID, SC.StartTime, SC.Duration
from
(
select
Su.SubjectName, S.ClassRoomID,
S.StartTime, S.Duration, S.ClassID
from
Subjects Su
inner join
Classes S
on Su.SubjectID = S.SubjectID
) SC
left join
(
select
SS.ClassID
from
Student_Schedules SS
inner join
Student_Class_Status SCS
on SS.ClassStatus = SCS.ClassStatus
where
SCS.ClassStatus = 1
) as SE
on SE.ClassID = SC.ClassID
where
SE.ClassID is null;
-------------------------------------------------------------------
select distinct
SC.SubjectName
from
(
select
C.ClassID, S.SubjectName
from
Subjects S
inner join
Classes C
on S.SubjectID = C.SubjectID
) SC
left join
Faculty_Classes FC
on FC.ClassID = SC.ClassID
where
FC.ClassID is null;
-------------------------------------------------------------------
select
S.StudLastName, S.StudFirstName
from
Students S
left join
(
select
ST.StudentID
from
Students ST
left join
Student_Schedules SS
on SS.StudentID = ST.StudentID
left join
Student_Class_Status SCS
on SS.ClassStatus = SCS.ClassStatus
where
SCS.ClassStatus = 1
) SSS
on S.StudentID = SSS.StudentID
where
SSS.StudentID is null;
-------------------------------------------------------------------
select
S.StaffID, S.StfFirstName, S.StfLastname, CFC.ClassID
from
Staff S
left join
(
select
FC.StaffID, C.ClassID
from
Faculty_Classes FC
inner join
Classes C
on C.ClassID = FC.ClassID
) CFC
on S.StaffID = CFC.StaffID;
-------------------------------------------------------------------
use BowlingLeagueExample;
-------------------------------------------------------------------
???
---------------------------------------------------------------------
--Getting 169 rows, but book says 176 rows
select
TM.TourneyID, T.TourneyDate, T.TourneyLocation,
MG.MatchID, MG.GameNumber, MG.WinningTeamID
from
Tourney_Matches TM
left join
Tournaments T
on T.TourneyID = TM.TourneyID
left join
Match_Games MG
on TM.MatchID = MG.MatchID;
-------------------------------------------------------------------
use RecipesExample;
-------------------------------------------------------------------
???
-------------------------------------------------------------------
select
I.IngredientName, RR.RecipeTitle
from
Ingredients I
left join
(
select
R.RecipeTitle, R.RecipeID, RI.IngredientID
from
Recipes R
inner join
Recipe_Ingredients RI
on R.RecipeID = RI.RecipeID
) RR
on I.IngredientID = RR.IngredientID;
-------------------------------------------------------------------
select
RC.RecipeClassDescription, R.RecipeTitle
from
Recipe_Classes RC
left join
Recipes R
on R.RecipeClassID = RC.RecipeClassID
where
RC.RecipeClassID in (1,4,7);
-------------------------------------------------------------------
select
RC.RecipeClassDescription, R.RecipeTitle
from
Recipe_Classes RC
left join
Recipes R
on R.RecipeClassID = RC.RecipeClassID;
------------------------------------------------------------------
T H E E N D
------------------------------------------------------------------