-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-7-MATCHING.sql
251 lines (240 loc) · 6.08 KB
/
module-7-MATCHING.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
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
/* Creating the other tables and
seeding data for the JOIN module */
CREATE TABLE address (
address_id INTEGER NOT NULL,
address_building_number VARCHAR(55) NOT NULL,
address_street VARCHAR(55) NOT NULL,
address_locality VARCHAR(55),
address_city VARCHAR(55) NOT NULL,
address_zip_postal VARCHAR(55) NOT NULL,
address_state_province_county VARCHAR(55) NOT NULL,
address_country VARCHAR(55) NOT NULL,
CONSTRAINT PK_Address PRIMARY KEY (address_id)
);
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (1, '7000', 'Hollywood Blvd', NULL, 'Los Angeles', '90028', 'California', 'US');
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (2, '5299', 'Los Caballeros Way', NULL, 'Los Angeles', '90027', 'California', 'US');
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (3, '5299', 'Los Caballeros Way', NULL, 'Los Angeles', '90027', 'California', 'US');
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (4, '2016', 'Canyon Dr', NULL, 'Los Angeles', '90068', 'California', 'US');
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (5, '5983', 'Franklin Ave', NULL, 'Los Angeles', '90028', 'California', 'US');
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (6, '755', 'Pine St', NULL, 'San Francisco', '94108', 'California', 'US');
INSERT INTO address (
address_id,
address_building_number,
address_street,
address_locality,
address_city,
address_zip_postal,
address_state_province_county,
address_country)
VALUES (7, '528', 'Lombard St', NULL, 'San Francisco', '94133', 'California', 'US');
CREATE TABLE email_address (
email_address_id INTEGER NOT NULL,
email_address_person_id INTEGER,
email_address VARCHAR(55) NOT NULL,
CONSTRAINT PK_email_address PRIMARY KEY (email_address_id)
);
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (1, 1, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (2, 1, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (3, 2, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (4, 2, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (5, 3, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (6, 4, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (7, 4, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (8, 5, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (9, 6, '[email protected]');
INSERT INTO email_address (
email_address_id,
email_address_person_id,
email_address)
VALUES (10, NULL, '[email protected]');
CREATE TABLE person_address (
person_address_id INTEGER NOT NULL,
person_address_person_id INTEGER NOT NULL,
person_address_address_id INTEGER NOT NULL,
CONSTRAINT PK_person_address PRIMARY KEY (person_address_id)
);
CREATE TABLE phone_number (
phone_number_id INTEGER NOT NULL,
phone_number_person_id INTEGER NOT NULL,
phone_number VARCHAR(55) NOT NULL,
CONSTRAINT PK_phone_number PRIMARY KEY (phone_number_id)
);
/* -------------------------- */
/* BAD! DON'T DO IT
SELECT
p.person_id,
p.person_first_name,
ea.email_address
FROM
person p
CROSS JOIN
email_address ea;
*/
SELECT
p.person_id,
CONCAT(p.person_first_name, ' ', p.person_last_name) AS FullName,
ea.email_address
FROM
person p
INNER JOIN
email_address ea
ON ea.email_address_person_id = p.person_id
ORDER BY
p.person_first_name;
SELECT
CONCAT(p.person_first_name, ' ', p.person_last_name) AS FullName,
COUNT(ea.email_address) AS EmailCount
FROM
person p
INNER JOIN
email_address ea
ON ea.email_address_person_id = p.person_id
GROUP BY
p.person_id
HAVING
EmailCount > 1
ORDER BY
FullName;
SELECT
p.person_id,
p.person_first_name,
p.person_last_name,
ea.email_address
FROM
person p
LEFT OUTER JOIN
email_address ea
ON ea.email_address_person_id = p.person_id
WHERE
p.person_contacted_number > 2
ORDER BY
p.person_id;
SELECT
p.person_id,
p.person_first_name,
p.person_last_name,
ea.email_address_id,
ea.email_address
FROM
person p
RIGHT OUTER JOIN
email_address ea
ON p.person_id = ea.email_address_person_id
ORDER BY
p.person_id;
/* A workaround to MySQL's lack of
support for FULL OUTER JOIN's,
using UNION DISTINCT */
SELECT
p.person_id,
p.person_first_name,
p.person_last_name,
ea.email_address_id,
ea.email_address
FROM
person p
LEFT JOIN
email_address ea
ON p.person_id = ea.email_address_person_id
UNION DISTINCT
SELECT
p.person_id,
p.person_first_name,
p.person_last_name,
ea.email_address_id,
ea.email_address
FROM
person p
RIGHT JOIN
email_address ea
ON ea.email_address_person_id = p.person_id