-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW02ReviewSQLandDDL.sql
More file actions
313 lines (281 loc) · 7.95 KB
/
Copy pathW02ReviewSQLandDDL.sql
File metadata and controls
313 lines (281 loc) · 7.95 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
-- W02 Review: SQL and DDL
-- Gabriel Yanqui
-- Sales Orders Database
USE SalesOrdersExample;
-- 1. “List customers and the dates they placed an order, sorted in order date sequence.”
-- (Hint: The solution requires a JOIN of two tables.)
SELECT
CustFirstName,
CustLastName,
OrderDate
FROM
office.customers c
JOIN
office.Orders o ON c.CustomerID = o.CustomerID
ORDER BY
OrderDate;
-- 2. “List employees and the customers for whom they booked an order.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT DISTINCT
e.EmpLastName,
e.EmpFirstName,
c.CustLastName,
c.CustFirstName
FROM
office.Employees e
JOIN
office.Orders o ON e.EmployeeID = o.EmployeeID
JOIN
office.Customers c ON o.CustomerID = c.CustomerID;
-- 3. “Display all orders, the products in each order, and the amount owed for each product, in order number sequence.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT
o.OrderNumber,
o.OrderDate,
p.ProductName,
od.QuantityOrdered,
od.QuotedPrice,
(od.QuantityOrdered * od.QuotedPrice) AS AmountOwed
FROM
office.Orders o
JOIN
office.Order_Details od ON o.OrderNumber = od.OrderNumber
JOIN
dbo.Products p ON od.ProductNumber = p.ProductNumber
ORDER BY
o.OrderNumber;
-- 4. “Show me the vendors and the products they supply to us for products that cost less than $100.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT
v.VendorID,
v.VendName,
p.ProductName,
pv.WholesalePrice
FROM
dbo.Vendors v
JOIN
dbo.Product_Vendors pv ON v.VendorID = pv.VendorID
JOIN
dbo.Products p ON pv.ProductNumber = p.ProductNumber
WHERE
pv.WholesalePrice < 100
ORDER BY
pv.WholesalePrice;
-- 5. “Show me customers and employees who have the same last name.”
-- (Hint: The solution requires a JOIN on matching values.)
SELECT
c.CustLastName,
c.CustFirstName,
e.EmpLastName,
e.EmpFirstName
FROM
office.Customers c
JOIN
office.Employees e ON c.CustLastName = e.EmpLastName;
-- 6. “Show me customers and employees who live in the same city.”
-- (Hint: The solution requires a JOIN on matching values.)
SELECT
c.CustLastName,
c.CustFirstName,
e.EmpLastName,
e.EmpFirstName
FROM
office.Customers c
JOIN
office.Employees e ON c.CustCity = e.EmpCity;
-- Entertainment Agency Database
USE EntertainmentAgencyExample;
-- 1. “Display agents and the engagement dates they booked, sorted by booking start date.”
-- (Hint: The solution requires a JOIN of two tables.)
SELECT
AgtFirstName,
AgtLastName,
StartDate,
EndDate
FROM
dbo. Agents a
JOIN
dbo.Engagements e ON a.AgentID = e.AgentID
ORDER BY
StartDate;
-- 2. “List customers and the entertainers they booked.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT DISTINCT
c.CustFirstName,
c.CustLastName,
ent.EntStageName
FROM
dbo.Customers c
JOIN
dbo.Engagements eng ON c.CustomerID = eng.CustomerID
JOIN
dbo.Entertainers ent ON eng.EntertainerID = ent.EntertainerID;
-- 3. “Find the agents and entertainers who live in the same postal code.”
-- (Hint: The solution requires a JOIN on matching values.)
SELECT
AgtFirstName,
AgtLastName,
EntStageName
FROM
dbo.Agents a
JOIN
dbo.Entertainers e ON a.AgtZipCode = e.EntZipCode;
-- School Scheduling Database
USE SchoolSchedulingExample;
-- 1. “Display buildings and all the classrooms in each building.”
-- (Hint: The solution requires a JOIN of two tables.)
SELECT
BuildingName,
ClassRoomID
FROM
dbo.Buildings b
JOIN
dbo.Class_Rooms cr ON b.BuildingCode = cr.BuildingCode;
--2. “List students and all the classes in which they are currently enrolled.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT
s.StudFirstName,
s.StudLastName,
c.ClassID,
scs.ClassStatusDescription
FROM
Students s
JOIN
Student_Schedules ss ON s.StudentID = ss.StudentID
JOIN
Classes c ON ss.ClassID = c.ClassID
JOIN
Student_Class_Status scs ON ss.ClassStatus = scs.ClassStatus
WHERE
scs.ClassStatus = '1';
-- 3. “List the faculty staff and the subject each teaches.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT
s.StfFirstName,
s.StfLastName,
sub.SubjectName
FROM
Staff s
JOIN
Faculty_Subjects fs ON s.StaffID = fs.StaffID
JOIN
Subjects sub ON fs.SubjectID = sub.SubjectID;
-- 4. “Show me the students who have a grade of 85 or better in art and who also have a grade of 85 or better in any computer course.”
-- (Hint: The solution requires a JOIN on matching values.)
SELECT
StudFirstName,
StudLastName,
Grade
FROM
Students s
JOIN
Student_Schedules ss ON s.StudentID = ss.StudentID
JOIN
Classes c ON ss.ClassID = c.ClassID
JOIN
Subjects sub ON c.SubjectID = sub.SubjectID
WHERE
(sub.SubjectName LIKE '%art%' AND ss.Grade >= '85')
AND (sub.SubjectName LIKE '%computer%' AND ss.Grade >= '85');
-- Bowling League Database
USE BowlingLeagueExample;
-- 1. “List the bowling teams and all the team members.”
-- (Hint: The solution requires a JOIN of two tables.)
SELECT
TeamName,
CONCAT (BowlerFirstName, ' ' , BowlerLastName) TeamMemberName
FROM
Teams t
JOIN
Bowlers b ON t.TeamID = b.TeamID
-- 2. “Display the bowlers, the matches they played in, and the bowler game scores.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT
b.BowlerFirstName,
b.BowlerLastName,
bs.MatchID,
bs.RawScore,
bs.HandiCapScore
FROM
bowlers b
JOIN
bowler_scores bs ON bs.BowlerID = b.BowlerID;
-- 3. “Find the bowlers who live in the same ZIP Code.”
-- (Hint: The solution requires a JOIN on matching values, and be sure to not match bowlers with themselves.)
SELECT
CONCAT(b1.BowlerFirstName, ' ' , b1.BowlerLastName) AS Bowler,
CONCAT(b2.BowlerFirstName, ' ', b2.BowlerLastName) AS OtherBowler
FROM
Bowlers b1
JOIN
Bowlers b2 ON b1.BowlerZip = b2.BowlerZip
WHERE
b1.BowlerID <> b2.BowlerID
-- Recipes Database
USE RecipesExample;
-- 1. “List all the recipes for salads.”
-- (Hint: The solution requires a JOIN of two tables.)
SELECT
*
FROM
Recipes r
JOIN
Recipe_Classes rc ON r.RecipeClassID = rc.RecipeClassID
WHERE
rc.RecipeClassDescription = 'Salad';
-- 2. “List all recipes that contain a dairy ingredient.”
-- (Hint: The solution requires a JOIN of more than two tables.)
SELECT DISTINCT
r.RecipeTitle
FROM
Recipes r
JOIN
Recipe_Ingredients ri ON r.recipeID = ri.RecipeID
JOIN
Ingredients i ON ri.IngredientID = i.IngredientID
JOIN
Ingredient_Classes ic ON i.IngredientClassID = ic.IngredientClassID
WHERE
ic.IngredientClassDescription = 'dairy';
-- 3. “Find the ingredients that use the same default measurement amount.”
-- (Hint: The solution requires a JOIN on matching values.)
SELECT
one.FirstIngredient,
one.Measurement,
two.SecondIngredient
FROM
(SELECT
i1.IngredientName AS FirstIngredient,
m1.MeasurementDescription AS Measurement
FROM
ingredients i1
JOIN
measurements m1 ON i1.MeasureAmountID = m1.MeasureAmountID) AS one
JOIN
(SELECT
i2.IngredientName AS SecondIngredient,
m2.MeasurementDescription AS Measurement
FROM
ingredients i2
JOIN
measurements m2 ON i2.MeasureAmountID = m2.MeasureAmountID) AS two
ON
one.Measurement = two.Measurement
WHERE
one.FirstIngredient <> two.SecondIngredient;
-- 4. “Show me the recipes that have beef and garlic.”
-- (Hint: The solution requires a JOIN on matching values.)
SELECT DISTINCT
r.RecipeTitle
FROM
Recipes r
JOIN
Recipe_Ingredients ri ON r.RecipeID = ri.RecipeID
JOIN
Ingredients i ON ri.IngredientID = i.IngredientID
WHERE
i.IngredientName IN ('Beef', 'Garlic')
GROUP BY
r.RecipeID, r.RecipeTitle
HAVING
COUNT(DISTINCT i.IngredientName) = 2;