-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCombining and Joining Tables Lesson.sql
209 lines (173 loc) · 4.42 KB
/
Combining and Joining Tables Lesson.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
/*
Combining Tables
Simplest examples of merging and appending tables
*/
-- View the contents of the tables
SELECT
fx.CountryCode
, fx.CurrencyCode
FROM
CurrencyFX fx;
SELECT
g.CountryCode
, g.CapitalCity
FROM
Geography g;
-- An INNER JOIN only keeps rows with matching values in both left and right tables
-- The INNER JOIN is the default JOIN; best to specify exactly what you want
SELECT
g.CountryCode AS GeoCountryCode
, g.CapitalCity
, fx.CountryCode AS FxCountryCode
, fx.CurrencyCode
FROM
Geography g INNER JOIN CurrencyFX fx ON fx.CountryCode = g.CountryCode;
/*
A LEFT JOIN returns all rows from the left table and matching values from the right table.
If there is no matching value from the right table, the result table has NULL values in the columns originating in the right table
*/
SELECT
g.CountryCode AS GeoCountryCode
, g.CapitalCity
, fx.CountryCode AS FxCountryCode
, fx.CurrencyCode
FROM
Geography g LEFT JOIN CurrencyFX fx ON fx.CountryCode = g.CountryCode;
/*
A RIGHT JOIN returns all rows from the right table and matching values from the left table.
If there is no matching value from the left table, the result table has NULL values in the columns originating in the left table
*/
SELECT
g.CountryCode AS GeoCountryCode
, g.CapitalCity
, fx.CountryCode AS FxCountryCode
, fx.CurrencyCode
FROM Geography g RIGHT JOIN CurrencyFX fx ON fx.CountryCode = g.CountryCode;
/*
<Table B> RIGHT JOIN <Table A> gives same results as <Table A> LEFT JOIN <Table B>
For this reason, right joins are rarely used
*/
SELECT
g.CountryCode AS GeoCountryCode
, g.CapitalCity
, fx.CountryCode AS FxCountryCode
, fx.CurrencyCode
FROM CurrencyFX fx RIGHT JOIN Geography g ON fx.CountryCode = g.CountryCode;
/*
A FULL JOIN returns all rows from both tables.
If there is no match from the either table, the result table has NULL values in the columns originating in the unmatched table
FULL JOIN and FULL OUTER JOIN are exactly the same.
*/
SELECT
g.CountryCode AS GeoCountryCode
, g.CapitalCity
, fx.CountryCode AS FxCountryCode
, fx.CurrencyCode
FROM
Geography g
FULL OUTER JOIN CurrencyFX fx ON fx.CountryCode = g.CountryCode;
/*
A CROSS JOIN returns every combination of rows (cartesian product).
*/
SELECT
g.CountryCode AS GeoCountryCode
, g.CapitalCity
, fx.CountryCode AS FxCountryCode
, fx.CurrencyCode
FROM
Geography g CROSS JOIN CurrencyFX fx;
/*
A semi-join returns the rows from the first tables that have matching values in the second table.
It only returns the columns from the first table
There is no SQL SEMI JOIN operator, use IN() or EXISTS() to accomplish a semi-join
*/
SELECT
g.CountryCode
, g.CapitalCity
FROM
Geography g
WHERE
g.CountryCode IN
( SELECT fx.CountryCode FROM CurrencyFX fx );
SELECT
g.CountryCode
, g.CapitalCity
FROM
Geography g
WHERE
EXISTS
( SELECT * FROM CurrencyFX fx WHERE fx.CountryCode = g.CountryCode);
/*
A anti-join returns the rows from the first table that do not have matching values in the second table.
It only returns the columns from the first table
There is no SQL ANTI JOIN operator, use NOT IN() or NOT EXISTS() to accomplish a semi-join
*/
SELECT
g.CountryCode
, g.CapitalCity
FROM
Geography g
WHERE
g.CountryCode NOT IN
( SELECT fx.CountryCode FROM CurrencyFX fx );
SELECT
g.CountryCode
, g.CapitalCity
FROM
Geography g
WHERE
NOT EXISTS
( SELECT * FROM CurrencyFX fx WHERE fx.CountryCode = g.CountryCode);
-- UNION appends (or stacks) two queries removing any duplicates
SELECT
CountryCode
FROM
Geography g
UNION
SELECT
CountryCode
FROM
CurrencyFX fx;
-- UNION ALL appends (or stacks) two queries keeping any duplicates
SELECT
CountryCode
FROM
Geography g
UNION ALL
SELECT
CountryCode
FROM
CurrencyFX fx;
-- INTERSECT returns rows with values found in both queries
SELECT
CountryCode
FROM
Geography g
INTERSECT
SELECT
CountryCode
FROM
CurrencyFX fx;
-- This is eqivalent to the following INNER JOIN
SELECT
g.CountryCode
FROM
Geography g INNER JOIN CurrencyFX fx ON g.CountryCode = fx.CountryCode;
-- EXCEPT returns the rows from the first query that are not in the second query
-- It provides a similar result to the left anti-join
SELECT
CountryCode
FROM
Geography g
EXCEPT
SELECT CountryCode FROM CurrencyFX fx;
/*
CREATE TABLE Geography
(CountryCode CHAR(2) NOT NULL PRIMARY key,
CapitalCity VARCHAR(50) NOT null
)
CREATE TABLE CurrencyFX
(CountryCode CHAR(2) NOT NULL PRIMARY key,
CurrencyCode VARCHAR(50) NOT null
)
*/