Repository navigation
Expand file tree
/
Copy path6_Joins_Solutions.sql
More file actions
280 lines (216 loc) · 6.93 KB
/
Copy path6_Joins_Solutions.sql
File metadata and controls
280 lines (216 loc) · 6.93 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
/******************************************************************************
Course videos: https://www.red-gate.com/hub/university/courses/t-sql/tsql-for-beginners
Course scripts: https://litknd.github.io/TSQLBeginners
JOINs: INNER, OUTER, FULL, CROSS
SOLUTION FILE
*****************************************************************************/
/* Doorstop */
RAISERROR(N'Did you mean to run the whole thing?', 20, 1) WITH LOG;
GO
/******************************************************************************
Homework with Solutions
*****************************************************************************/
USE WideWorldImporters;
GO
--The questions in this homework refer to two tables:
EXEC sp_help 'Application.StateProvinces';
GO
EXEC sp_help 'Application.Countries';
GO
--Join column: CountryID
/*
Q1
Return two columns:
StateProvinceName, CountryName
Return rows for all StateProvinces which have a related CountryID.
This should return 53 rows.
*/
--INNER JOIN: return all matching rows
SELECT p.StateProvinceName,
c.CountryName
FROM Application.StateProvinces AS p
INNER JOIN Application.Countries AS c
ON c.CountryID = p.CountryID;
GO
--INNER is the default, and is often omitted
SELECT p.StateProvinceName,
c.CountryName
FROM Application.StateProvinces AS p
JOIN Application.Countries AS c
ON c.CountryID = p.CountryID;
/*
Demo:
What if we reverse the order in which we list the tables?
Does it change the way the query is run behind the scenes?
*/
SELECT p.StateProvinceName,
c.CountryName
FROM Application.Countries AS c
JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
/*
Q2
Return two columns:
StateProvinceName, CountryName
Return rows for all Countries, with a related StateProvinceName IF it exists.
The query should return 242 rows.
Write two versions of the query, one with a LEFT OUTER JOIN,
and one with a RIGHT OUTER JOIN
*/
--LEFT OUTER: return all the rows in the "LEFT" table (Application.Countries)
--add matching rows in the "RIGHT" table (Application.StateProvinces) when possible
SELECT p.StateProvinceName,
c.CountryName
FROM Application.Countries AS c
LEFT OUTER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
--RIGHT OUTER: return all the rows in the "RIGHT" table (Application.Countries)
--add matching rows in the "LEFT" table (Application.StateProvinces) when possible
SELECT p.StateProvinceName,
c.CountryName
FROM Application.StateProvinces AS p
RIGHT OUTER JOIN Application.Countries AS c
ON c.CountryID = p.CountryID;
GO
--OUTER is optional, and is often omitted
SELECT p.StateProvinceName,
c.CountryName
FROM Application.StateProvinces AS p
RIGHT JOIN Application.Countries AS c
ON c.CountryID = p.CountryID;
GO
/*
Q3
Return one column:
CountryName
Return rows for all CountryNames which do NOT have a related row in StateProvinces.
The query should return 189 rows.
*/
--Pattern: OUTER JOIN + WHERE clause filter
/*
https://docs.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql
Logical Query Processing Order
FROM
ON
JOIN
WHERE
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
*/
SELECT c.CountryName, c.CountryID
FROM Application.Countries AS c
--LEFT OUTER: Return all rows in the "LEFT" table (Application.Countries)
--add matching rows in the "RIGHT" table (Application.StateProvinces) when possible
LEFT OUTER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID
--WHERE clause only includes rows which don't have an Application.StateProvinces.CountryID
WHERE p.CountryID IS NULL
--This can be written with a RIGHT outer join as well
SELECT c.CountryName
FROM Application.StateProvinces AS p
RIGHT OUTER JOIN Application.Countries AS c
ON c.CountryID = p.CountryID
WHERE p.StateProvinceID IS NULL;
GO
--Discussion: This rewrite of putting the "IS NULL" in the OUTER JOIN itself does NOT work
--Tip: the 'United States' is the only country with related State Provinces, so if it's in the list this did NOT work
SELECT c.CountryName, c.CountryID
FROM Application.Countries AS c
LEFT OUTER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID
and p.CountryID IS NULL;
GO
--Similarly....
SELECT c.CountryName, c.CountryID
FROM Application.Countries AS c
LEFT OUTER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID
and c.CountryID <> p.CountryID;
GO
/*
Q4
Return one column:
CountryName
Return rows for a DISTINCT list of countries which DO have a related row in StateProvinces.
This should return one row.
*/
SELECT DISTINCT
c.CountryName
FROM Application.Countries AS c
INNER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID;
GO
/*
Q5
Return two columns:
CountryName, StateProvinceName
Return rows for all CountryNames and related StateProvinceNames
even if the Country does not have a related StateProvince
or even if the StateProvince does not have a related country
*/
/* Order of the tables specified does not matter */
SELECT c.CountryName,
p.StateProvinceName
FROM Application.Countries AS c
FULL OUTER JOIN Application.StateProvinces AS p
ON c.CountryID = p.CountryID
ORDER BY c.CountryName,
p.StateProvinceName,
p.StateProvinceID;
GO
/*
Q6
Return three columns: CountryName, StateProvinceName, StateProvinceId
List every possible combination of:
* CountryName
* StateProvinceName, StateProvinceID
Order the results by:
CountryName, StateProvinceName, StateProvinceID
Application.Countries has 190 rows, Application.StateProvinces has 53 rows
So this should return 190 x 53 = 10070 rows
*/
SELECT c.CountryName,
p.StateProvinceName,
p.StateProvinceID
FROM Application.Countries AS c
CROSS JOIN Application.StateProvinces AS p
ORDER BY c.CountryName,
p.StateProvinceName,
p.StateProvinceID;
GO
/*
Q7
SELECT four columns:
Column 1: The CountryName for CountryID = 100
Column 2: The LatestRecordedPopulation for CountryID = 100
Column 3: The CountryName for the Country whose LatestRecordedPopulation
is less than, but closest to that of CountryID = 100
Column 4: The LatestRecordedPopulation for the Country in Column 3 (next lowest population)
Return one row (where CountryID = 100).
Use a self-join to get this result.
*/
SELECT TOP 1
c1.CountryName,
c1.LatestRecordedPopulation,
c2.CountryName,
c2.LatestRecordedPopulation
FROM Application.Countries AS c1
JOIN Application.Countries AS c2
ON c2.LatestRecordedPopulation < c1.LatestRecordedPopulation
WHERE c1.CountryID = 100
ORDER BY c2.LatestRecordedPopulation DESC;
GO
--Simple query to check your work
SELECT CountryID,
CountryName,
LatestRecordedPopulation
FROM Application.Countries
ORDER BY LatestRecordedPopulation DESC;
GO