Repository navigation
Expand file tree
/
Copy path12_CTEsSubqueriesDerivedTables_Solutions.sql
More file actions
316 lines (247 loc) · 8.04 KB
/
Copy path12_CTEsSubqueriesDerivedTables_Solutions.sql
File metadata and controls
316 lines (247 loc) · 8.04 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
/******************************************************************************
Course videos: https://www.red-gate.com/hub/university/courses/t-sql/tsql-for-beginners
Course scripts: https://litknd.github.io/TSQLBeginners
CTEs, Subqueries, and Derived Tables: oh, my!
SOLUTION FILE
*****************************************************************************/
/* ✋🏻 Doorstop ✋🏻 */
RAISERROR(N'Did you mean to run the whole thing?', 20, 1) WITH LOG;
GO
USE WideWorldImporters;
GO
--Run this to simplify execution plans
--https://littlekendra.com/2017/01/26/whats-that-garbage-in-my-execution-plan-dear-sql-dba-episode-26/
DROP SECURITY POLICY IF EXISTS [Application].[FilterCustomersBySalesTerritoryRole];
GO
/******************************************************************************
The first four questions challenge you to write the SAME query four different ways
*****************************************************************************/
/*
Q1
Using the Applications.Countries and Applications.StateProvinces tables
Return a distinct list of CountryNames for countries which DO have a related row in StateProvinces
Return one column: CountryName
USE an IN clause with a subquery in your WHERE clause
Do not use DISTINCT or EXISTS, do not use a CTE or a derived table
This should return one row.
*/
SELECT c.CountryName
FROM Application.Countries AS c
WHERE CountryID IN
(
SELECT CountryID FROM Application.StateProvinces
);
--Compare the plan to:
SELECT DISTINCT
c.CountryName
FROM Application.Countries AS c
INNER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
/*
Q2
Using the Applications.Countries and Applications.StateProvinces tables
Return a distinct list of CountryNames for countries which DO have a related row in StateProvinces
Return one column: CountryName
Join to a derived table to get back the DISTINCT list of CountryIDs which are in Applications.StateProvinces
Do not use IN
Use DISTINCT only inside the derived table definition, do not use a CTE.
This should return one row.
*/
SELECT c.CountryName
FROM Application.Countries AS c
JOIN
(SELECT DISTINCT CountryID FROM Application.StateProvinces) AS sp
ON c.CountryID = sp.CountryID
--Compare the plan to:
SELECT DISTINCT
c.CountryName
FROM Application.Countries AS c
INNER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
--What if we didn't use DISTINCT?
SELECT c.CountryName
FROM Application.Countries AS c
JOIN
(SELECT CountryID FROM Application.StateProvinces) AS sp
ON c.CountryID = sp.CountryID
/*
Q3
Using the Applications.Countries and Applications.StateProvinces tables
Return a distinct list of CountryNames for countries which DO have a related row in StateProvinces
Return one column: CountryName
Join to a cte to get back the DISTINCT list of CountryIDs which are in Applications.StateProvinces
Do not use IN
Only use DISTINCT inside the cte definition, do not use a derived table
This should return one row.
*/
WITH sp
AS (SELECT DISTINCT
CountryID
FROM Application.StateProvinces)
SELECT c.CountryName
FROM Application.Countries AS c
JOIN sp
ON c.CountryID = sp.CountryID;
--Compare the plan to:
SELECT DISTINCT
c.CountryName
FROM Application.Countries AS c
INNER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
/*
Q4
Using the Applications.Countries and Applications.StateProvinces tables
Return a distinct list of CountryNames for countries which DO have a related row in StateProvinces
Return one column: CountryName
USE an EXISTS with a correlated subquery in your WHERE clause
Do not use DISTINCT or IN, do not use a CTE or a derived table.
This should return one row.
*/
SELECT c.CountryName
FROM Application.Countries AS c
WHERE EXISTS
(
SELECT 1
FROM Application.StateProvinces AS p
WHERE c.CountryID = p.CountryID
);
--Compare the plan to:
SELECT DISTINCT
c.CountryName
FROM Application.Countries AS c
INNER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
/* Question: is this really a "correlated" or "repeating" subquery? */
--Weird trick: run the whole query
--Then run only : SELECT 1/0
--Why does the whole query work?!?!
SELECT c.CountryName
FROM Application.Countries AS c
WHERE EXISTS
(
SELECT 1/0
FROM Application.StateProvinces AS p
WHERE c.CountryID = p.CountryID
);
/*
Q5 & Q6
Using the Sales.Orders, Sales.Customers, and Sales.Cities tables...
Return 4 columns:
CityID
CityName
CustomerCount - defined as the count of unique customers in that CityID
OrderCount - defined as the count of unique OrderIDs for Customers whose DeliveryCityID matches that CityID
Only return rows for CityIDs having > 1 CustomerID
Do return
Write this query two ways:
Q5 First, with two CTES:
One CTE queries the OrderCount for the DeliveryCityID
Another CTE queries the CityID, CityName, and CustomerCount for CityIDs with > 1 CustomerID
Join the two CTEs to get the results
Q6 Next, rewrite this with derived tables instead of CTES:
One derived table queries the OrderCount for the DeliveryCityID
Another derived table queries the CityID, CityName, and CustomerCount for CityIDs with > 1 CustomerID
Join the two derived tables to get the results
Each query should return 8 rows
*/
WITH orders
AS (SELECT sc.DeliveryCityID,
COUNT(OrderID) AS OrderCount
FROM Sales.Orders AS so
JOIN Sales.Customers AS sc
ON so.CustomerID = sc.CustomerID
GROUP BY sc.DeliveryCityID),
deliverycities
AS (SELECT ct.CityID,
ct.CityName,
COUNT(c.CustomerID) AS CustomerCount
FROM Sales.Customers AS c
JOIN Application.Cities AS ct
ON c.DeliveryCityID = ct.CityID
GROUP BY ct.CityID,
ct.CityName
HAVING COUNT(c.CustomerID) > 1)
SELECT deliverycities.CityID,
deliverycities.CityName,
deliverycities.CustomerCount,
orders.OrderCount
FROM deliverycities
LEFT OUTER JOIN orders
ON deliverycities.CityID = orders.DeliveryCityID
ORDER BY CustomerCount DESC;
GO
SELECT deliverycities.CityID,
deliverycities.CityName,
deliverycities.CustomerCount,
orders.OrderCount
FROM
(
SELECT ct.CityID,
ct.CityName,
COUNT(c.CustomerID) AS CustomerCount
FROM Sales.Customers AS c
JOIN Application.Cities AS ct
ON c.DeliveryCityID = ct.CityID
GROUP BY ct.CityID,
ct.CityName
HAVING COUNT(c.CustomerID) > 1
) AS deliverycities
LEFT OUTER JOIN
(
SELECT sc.DeliveryCityID,
COUNT(OrderID) AS OrderCount
FROM Sales.Orders AS so
JOIN Sales.Customers AS sc
ON so.CustomerID = sc.CustomerID
GROUP BY sc.DeliveryCityID
) AS orders
ON deliverycities.CityID = orders.DeliveryCityID
ORDER BY CustomerCount DESC;
GO
/*
Q7 - Q9
Using only the Sales.Customers table
Return one column: CustomerName
Return the CustomerName(s) for the customer(s) who have the maximum CreditLimit in Sales.Customers
Write the query multiple ways:
Q7 using a subquery in the WHERE clause
Q8 using one CTE
Q9 using a derived table
*/
--using a subquery in the WHERE clause
SELECT CustomerName
FROM Sales.Customers
WHERE CreditLimit =
(
SELECT MAX(CreditLimit) FROM Sales.Customers
)
GO
SELECT CustomerName
FROM Sales.Customers
WHERE CreditLimit IN /* This implies more than one row,
and it's a MAX, which will only return one row --
but this still works. */
(
SELECT MAX(CreditLimit) FROM Sales.Customers
);
GO
--Q8 using one CTE
WITH maxcred
AS (SELECT MAX(CreditLimit) AS limit
FROM Sales.Customers)
SELECT c.CustomerName
FROM Sales.Customers AS c
JOIN maxcred
ON maxcred.limit = c.CreditLimit;
GO
--Q9 using a derived table
SELECT c.CustomerName
FROM Sales.Customers AS c
JOIN
(SELECT MAX(CreditLimit) AS limit FROM Sales.Customers) AS maxcred
ON maxcred.limit = c.CreditLimit;
GO