Repository navigation
Expand file tree
/
Copy path11_CTEsSubqueriesDerivedTables_Homework.sql
More file actions
192 lines (90 loc) · 3.91 KB
/
Copy path11_CTEsSubqueriesDerivedTables_Homework.sql
File metadata and controls
192 lines (90 loc) · 3.91 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
/******************************************************************************
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!
HOMEWORK 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.
*/
/*
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.
*/
/*
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.
*/
/*
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.
*/
/*
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:
T5.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
T5.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
*/
/*
Q7, Q8, 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
*/
--Q7 using a subquery in the WHERE clause
--Q8 using one CTE
--Q9 using a derived table