forked from ricksilva/mysql_cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_5.sql
702 lines (656 loc) · 19.1 KB
/
chapter_5.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
--
-- MySQL Crash Course
--
-- Chapter 5 – Joining Database Tables
--
-- You can copy and paste any of these commands into your favorite MySQL tool
-- (like MySQL Workbench) and run them in your own MySQL environment.
--
create database if not exists subway;
use subway;
create table country
(
country_code char(2),
country varchar(100),
primary key(country_code)
);
insert into country (country_code, country)
values
('AF', 'Afghanistan'),
('AX', 'Aland Islands'),
('AL', 'Albania'),
('DZ', 'Algeria'),
('AS', 'American Samoa'),
('AD', 'Andorra'),
('AO', 'Angola'),
('AI', 'Anguilla'),
('AQ', 'Antarctica'),
('AG', 'Antigua and Barbuda'),
('AR', 'Argentina'),
('AM', 'Armenia'),
('AW', 'Aruba'),
('AU', 'Australia'),
('AT', 'Austria'),
('AZ', 'Azerbaijan'),
('BS', 'Bahamas'),
('BH', 'Bahrain'),
('BD', 'Bangladesh'),
('BB', 'Barbados'),
('BY', 'Belarus'),
('BE', 'Belgium'),
('BZ', 'Belize'),
('BJ', 'Benin'),
('BM', 'Bermuda'),
('BT', 'Bhutan'),
('BO', 'Bolivia'),
('BQ', 'Bonaire'),
('BA', 'Bosnia and Herzegovina'),
('BW', 'Botswana'),
('BV', 'Bouvet Island'),
('BR', 'Brazil'),
('IO', 'British Indian Ocean Territory'),
('BN', 'Brunei Darussalam'),
('BG', 'Bulgaria'),
('BF', 'Burkina Faso'),
('BI', 'Burundi'),
('KH', 'Cambodia'),
('CM', 'Cameroon'),
('CA', 'Canada'),
('CV', 'Cape Verde'),
('KY', 'Cayman Islands'),
('CF', 'Central African Republic'),
('TD', 'Chad'),
('CL', 'Chile'),
('CN', 'China'),
('CX', 'Christmas Island'),
('CC', 'Cocos Islands'),
('CO', 'Colombia'),
('KM', 'Comoros'),
('CG', 'Congo'),
('CD', 'Congo, the Democratic Republic of the'),
('CK', 'Cook Islands'),
('CR', 'Costa Rica'),
('CI', 'Ivory Coast'),
('HR', 'Croatia'),
('CU', 'Cuba'),
('CW', 'Curasao'),
('CY', 'Cyprus'),
('CZ', 'Czech Republic'),
('DK', 'Denmark'),
('DJ', 'Djibouti'),
('DM', 'Dominica'),
('DO', 'Dominican Republic'),
('EC', 'Ecuador'),
('EG', 'Egypt'),
('SV', 'El Salvador'),
('GQ', 'Equatorial Guinea'),
('ER', 'Eritrea'),
('EE', 'Estonia'),
('ET', 'Ethiopia'),
('FK', 'Falkland Islands'),
('FO', 'Faroe Islands'),
('FJ', 'Fiji'),
('FI', 'Finland'),
('FR', 'France'),
('GF', 'French Guiana'),
('PF', 'French Polynesia'),
('TF', 'French Southern Territories'),
('GA', 'Gabon'),
('GM', 'Gambia'),
('GE', 'Georgia'),
('DE', 'Germany'),
('GH', 'Ghana'),
('GI', 'Gibraltar'),
('GR', 'Greece'),
('GL', 'Greenland'),
('GD', 'Grenada'),
('GP', 'Guadeloupe'),
('GU', 'Guam'),
('GT', 'Guatemala'),
('GG', 'Guernsey'),
('GN', 'Guinea'),
('GW', 'Guinea-Bissau'),
('GY', 'Guyana'),
('HT', 'Haiti'),
('HM', 'Heard Island and McDonald Islands'),
('VA', 'Holy See'),
('HN', 'Honduras'),
('HK', 'Hong Kong'),
('HU', 'Hungary'),
('IS', 'Iceland'),
('IN', 'India'),
('ID', 'Indonesia'),
('IR', 'Iran'),
('IQ', 'Iraq'),
('IE', 'Ireland'),
('IM', 'Isle of Man'),
('IL', 'Israel'),
('IT', 'Italy'),
('JM', 'Jamaica'),
('JP', 'Japan'),
('JE', 'Jersey'),
('JO', 'Jordan'),
('KZ', 'Kazakhstan'),
('KE', 'Kenya'),
('KI', 'Kiribati'),
('KP', 'North Korea'),
('KR', 'South Korea'),
('KW', 'Kuwait'),
('KG', 'Kyrgyzstan'),
('LA', 'Laos'),
('LV', 'Latvia'),
('LB', 'Lebanon'),
('LS', 'Lesotho'),
('LR', 'Liberia'),
('LY', 'Libya'),
('LI', 'Liechtenstein'),
('LT', 'Lithuania'),
('LU', 'Luxembourg'),
('MO', 'Macao'),
('MK', 'Macedonia'),
('MG', 'Madagascar'),
('MW', 'Malawi'),
('MY', 'Malaysia'),
('MV', 'Maldives'),
('ML', 'Mali'),
('MT', 'Malta'),
('MH', 'Marshall Islands'),
('MQ', 'Martinique'),
('MR', 'Mauritania'),
('MU', 'Mauritius'),
('YT', 'Mayotte'),
('MX', 'Mexico'),
('FM', 'Micronesia'),
('MD', 'Moldova'),
('MC', 'Monaco'),
('MN', 'Mongolia'),
('ME', 'Montenegro'),
('MS', 'Montserrat'),
('MA', 'Morocco'),
('MZ', 'Mozambique'),
('MM', 'Myanmar'),
('NA', 'Namibia'),
('NR', 'Nauru'),
('NP', 'Nepal'),
('NL', 'Netherlands'),
('NC', 'New Caledonia'),
('NZ', 'New Zealand'),
('NI', 'Nicaragua'),
('NE', 'Niger'),
('NG', 'Nigeria'),
('NU', 'Niue'),
('NF', 'Norfolk Island'),
('MP', 'Northern Mariana Islands'),
('NO', 'Norway'),
('OM', 'Oman'),
('PK', 'Pakistan'),
('PW', 'Palau'),
('PS', 'Palestine'),
('PA', 'Panama'),
('PG', 'Papua New Guinea'),
('PY', 'Paraguay'),
('PE', 'Peru'),
('PH', 'Philippines'),
('PN', 'Pitcairn'),
('PL', 'Poland'),
('PT', 'Portugal'),
('PR', 'Puerto Rico'),
('QA', 'Qatar'),
('RE', 'Reunion'),
('RO', 'Romania'),
('RU', 'Russian Federation'),
('RW', 'Rwanda'),
('BL', 'Saint Barthalemy'),
('SH', 'Saint Helena'),
('KN', 'Saint Kitts and Nevis'),
('LC', 'Saint Lucia'),
('MF', 'Saint Martin'),
('PM', 'Saint Pierre and Miquelon'),
('VC', 'Saint Vincent and the Grenadines'),
('WS', 'Samoa'),
('SM', 'San Marino'),
('ST', 'Sao Tome and Principe'),
('SA', 'Saudi Arabia'),
('SN', 'Senegal'),
('RS', 'Serbia'),
('SC', 'Seychelles'),
('SL', 'Sierra Leone'),
('SG', 'Singapore'),
('SK', 'Slovakia'),
('SI', 'Slovenia'),
('SB', 'Solomon Islands'),
('SO', 'Somalia'),
('ZA', 'South Africa'),
('GS', 'South Georgia'),
('SS', 'South Sudan'),
('ES', 'Spain'),
('LK', 'Sri Lanka'),
('SD', 'Sudan'),
('SR', 'Suriname'),
('SJ', 'Svalbard and Jan Mayen'),
('SZ', 'Swaziland'),
('SE', 'Sweden'),
('CH', 'Switzerland'),
('SY', 'Syrian Arab Republic'),
('TW', 'Taiwan'),
('TJ', 'Tajikistan'),
('TZ', 'Tanzania'),
('TH', 'Thailand'),
('TL', 'Timor-Leste'),
('TG', 'Togo'),
('TK', 'Tokelau'),
('TO', 'Tonga'),
('TT', 'Trinidad and Tobago'),
('TN', 'Tunisia'),
('TR', 'Turkey'),
('TM', 'Turkmenistan'),
('TC', 'Turks and Caicos Islands'),
('TV', 'Tuvalu'),
('UG', 'Uganda'),
('UA', 'Ukraine'),
('AE', 'United Arab Emirates'),
('GB', 'United Kingdom'),
('US', 'United States'),
('UM', 'United States Minor Outlying Islands'),
('UY', 'Uruguay'),
('UZ', 'Uzbekistan'),
('VU', 'Vanuatu'),
('VE', 'Venezuela'),
('VN', 'Viet Nam'),
('VI', 'Virgin Islands'),
('WF', 'Wallis and Futuna'),
('EH', 'Western Sahara'),
('YE', 'Yemen'),
('ZM', 'Zambia'),
('ZW', 'Zimbabwe');
create table subway_system
(
subway_system varchar(100),
city varchar(100),
country_code char(2)
);
insert into subway_system (subway_system, city, country_code)
values
('Algiers Metro', 'Algiers', 'DZ'),
('Buenos Aires Underground', 'Buenos Aires', 'AR'),
('Yerevan Metro', 'Yerevan', 'AM'),
('Sydney Metro', 'Sydney', 'AU'),
('Vienna U-Bahn', 'Vienna', 'AT'),
('Baku Metro', 'Baku', 'AZ'),
('Minsk Metro', 'Minsk', 'BY'),
('Brussels Metro', 'Brussels', 'BE'),
('Belo Horizonte Metro', 'Belo Horizonte', 'BR'),
('Brasilia Metro', 'Brasilia', 'BR'),
('Porto Alegre Metro', 'Porto Alegre', 'BR'),
('Recife Metro', 'Recife', 'BR'),
('Rio de Janeiro Metro', 'Rio de Janeiro', 'BR'),
('Salvador Metro', 'Salvador', 'BR'),
('Sao Paulo Metro', 'Sao Paulo', 'BR'),
('Sofia Metro', 'Sofia', 'BG'),
('Montreal Metro', 'Montreal', 'CA'),
('Toronto Subway', 'Toronto', 'CA'),
('SkyTrain', 'Vancouver', 'CA'),
('Santiago Metro', 'Santiago', 'CL'),
('Beijing Subway', 'Beijing', 'CN'),
('Changchun Subway', 'Changchun', 'CN'),
('Changsha Metro', 'Changsha', 'CN'),
('Changzhou Metro', 'Changzhou', 'CN'),
('Chengdu Metro', 'Chengdu', 'CN'),
('Chongqing Rail Transit', 'Chongqing', 'CN'),
('Dalian Metro', 'Dalian', 'CN'),
('Dongguan Rail Transit', 'Dongguan', 'CN'),
('Foshan Metro', 'Foshan', 'CN'),
('Fuzhou Metro', 'Fuzhou', 'CN'),
('Guangzhou Metro', 'Guangzhou', 'CN'),
('Guiyang Metro', 'Guiyang', 'CN'),
('Hangzhou Metro', 'Hangzhou', 'CN'),
('Harbin Metro', 'Harbin', 'CN'),
('Hefei Metro', 'Hefei', 'CN'),
('Hohhot Metro', 'Hohhot', 'CN'),
('Mass Transit Railway', 'Hong Kong', 'CN'),
('Jinan Metro', 'Jinan', 'CN'),
('Kunming Metro', 'Kunming', 'CN'),
('Lanzhou Metro', 'Lanzhou', 'CN'),
('Luoyang Subway', 'Luoyang', 'CN'),
('Nanchang Metro', 'Nanchang', 'CN'),
('Nanjing Metro', 'Nanjing', 'CN'),
('Nanning Metro', 'Nanning', 'CN'),
('Ningbo Rail Transit', 'Ningbo', 'CN'),
('Qingdao Metro', 'Qingdao', 'CN'),
('Shanghai Metro', 'Shanghai', 'CN'),
('Shaoxing Metro', 'Shaoxing', 'CN'),
('Shenyang Metro', 'Shenyang', 'CN'),
('Shenzhen Metro', 'Shenzhen', 'CN'),
('Shijiazhuang Metro', 'Shijiazhuang', 'CN'),
('Suzhou Rail Transit', 'Suzhou', 'CN'),
('Taiyuan Metro', 'Taiyuan', 'CN'),
('Tianjin Metro', 'Tianjin', 'CN'),
('Urumqi Metro', 'Urumqi', 'CN'),
('Wenzhou Rail Transit', 'Wenzhou', 'CN'),
('Wuhan Metro', 'Wuhan', 'CN'),
('Wuxi Metro', 'Wuxi', 'CN'),
('Xiamen Metro', 'Xiamen', 'CN'),
('Xian Metro', 'Xian', 'CN'),
('Xuzhou Metro', 'Xuzhou', 'CN'),
('Zhengzhou Metro', 'Zhengzhou', 'CN'),
('Medellin Metro', 'Medellin', 'CO'),
('Prague Metro', 'Prague', 'CZ'),
('Copenhagen Metro', 'Copenhagen', 'DK'),
('Santo Domingo Metro', 'Santo Domingo', 'DO'),
('Cairo Metro', 'Cairo', 'EG'),
('Helsinki Metro', 'Helsinki', 'FI'),
('Lille Metro', 'Lille', 'FR'),
('Lyon Metro', 'Lyon', 'FR'),
('Marseille Metro', 'Marseille', 'FR'),
('Paris Metro', 'Paris', 'FR'),
('Rennes Metro', 'Rennes', 'FR'),
('Toulouse Metro', 'Toulouse', 'FR'),
('Tbilisi Metro', 'Tbilisi', 'GE'),
('Berlin U-Bahn', 'Berlin', 'DE'),
('Hamburg U-Bahn', 'Hamburg', 'DE'),
('Munich U-Bahn', 'Munich', 'DE'),
('Nuremberg U-Bahn', 'Nuremberg', 'DE'),
('Athens Metro', 'Athens', 'GR'),
('Budapest Metro', 'Budapest', 'HU'),
('Ahmedabad Metro', 'Ahmedabad', 'IN'),
('Namma Metro', 'Bengaluru', 'IN'),
('Chennai Metro', 'Chennai', 'IN'),
('Delhi Metro', 'Delhi', 'IN'),
('Rapid Metro', 'Gurgaon', 'IN'),
('Hyderabad Metro', 'Hyderabad', 'IN'),
('Jaipur Metro', 'Jaipur', 'IN'),
('Kochi Metro', 'Kochi', 'IN'),
('Kolkata Metro', 'Kolkata', 'IN'),
('Lucknow Metro', 'Lucknow', 'IN'),
('Mumbai Metro', 'Mumbai', 'IN'),
('Nagpur Metro', 'Nagpur', 'IN'),
('Noida Metro', 'Noida', 'IN'),
('Jakarta MRT', 'Jakarta', 'ID'),
('Isfahan Urban Railway', 'Isfahan', 'IR'),
('Mashhad Urban Railway', 'Mashhad', 'IR'),
('Shiraz Metro', 'Shiraz', 'IR'),
('Tabriz Metro', 'Tabriz', 'IR'),
('Tehran Metro', 'Tehran', 'IR'),
('Brescia Metro', 'Brescia', 'IT'),
('Catania Metro', 'Catania', 'IT'),
('Genoa Metro', 'Genoa', 'IT'),
('Milan Metro', 'Milan', 'IT'),
('Naples Metro', 'Naples', 'IT'),
('Rome Metro', 'Rome', 'IT'),
('Turin Metro', 'Turin', 'IT'),
('Fukuoka City Subway', 'Fukuoka', 'JP'),
('Astram Line', 'Hiroshima', 'JP'),
('Kobe Municipal Subway', 'Kobe', 'JP'),
('Kyoto Municipal Subway', 'Kyoto', 'JP'),
('Nagoya Municipal Subway', 'Nagoya', 'JP'),
('Osaka Metro', 'Osaka', 'JP'),
('Sapporo Municipal Subway', 'Sapporo', 'JP'),
('Sendai Subway', 'Sendai', 'JP'),
('Toei Subway', 'Tokyo', 'JP'),
('Tokyo Metro', 'Tokyo', 'JP'),
('Rinkai Line', 'Tokyo', 'JP'),
('Yokohama Municipal Subway', 'Yokohama', 'JP'),
('Minatomirai Line', 'Yokohama', 'JP'),
('Almaty Metro', 'Almaty', 'KZ'),
('Pyongyang Metro', 'Pyongyang', 'KP'),
('Busan Metro', 'Busan', 'KR'),
('Daegu Metro', 'Daegu', 'KR'),
('Daejeon Metro', 'Daejeon', 'KR'),
('Gwangju Metro', 'Gwangju', 'KR'),
('Incheon Subway', 'Incheon', 'KR'),
('Seoul Subway', 'Seoul', 'KR'),
('Korail metro lines', 'Seoul', 'KR'),
('Shinbundang Line', 'Seoul', 'KR'),
('Rapid Rail', 'Kuala Lumpur', 'MY'),
('Tren Electrico Urbano', 'Guadalajara', 'MX'),
('Mexico City Metro', 'Mexico City', 'MX'),
('Metrorrey', 'Monterrey', 'MX'),
('Amsterdam Metro', 'Amsterdam', 'NL'),
('Rotterdam Metro', 'Rotterdam', 'NL'),
('Oslo Metro', 'Oslo', 'NO'),
('Lahore Metro', 'Lahore', 'PK'),
('Panama Metro', 'Panama City', 'PA'),
('Lima Metro', 'Lima', 'PE'),
('Manila Light Rail Transit System','Manila', 'PH'),
('Manila Metro Rail Transit System','Manila', 'PH'),
('Warsaw Metro', 'Warsaw', 'PL'),
('Lisbon Metro', 'Lisbon', 'PT'),
('Doha Metro', 'Doha', 'QA'),
('Bucharest Metro', 'Bucharest', 'RO'),
('Kazan Metro', 'Kazan', 'RU'),
('Moscow Metro', 'Moscow', 'RU'),
('Nizhny Novgorod Metro', 'Nizhny Novgorod', 'RU'),
('Novosibirsk Metro', 'Novosibirsk', 'RU'),
('Saint Petersburg Metro', 'Saint Petersburg', 'RU'),
('Samara Metro', 'Samara', 'RU'),
('Yekaterinburg Metro', 'Yekaterinburg', 'RU'),
('Mass Rapid Transit', 'Singapore', 'SG'),
('Barcelona Metro', 'Barcelona', 'ES'),
('Metro Bilbao', 'Bilbao', 'ES'),
('Madrid Metro', 'Madrid', 'ES'),
('Stockholm Metro', 'Stockholm', 'SE'),
('Lausanne Metro', 'Lausanne', 'CH'),
('Kaohsiung Rapid Transit', 'Kaohsiung', 'TW'),
('Taipei Metro', 'Taipei', 'TW'),
('Taichung Metro', 'Taichung', 'TW'),
('Taoyuan Metro', 'Taoyuan', 'TW'),
('BTS Skytrain', 'Bangkok', 'TH'),
('Metropolitan Rapid Transit', 'Bangkok', 'TH'),
('Adana Metro', 'Adana', 'TR'),
('Ankara Metro', 'Ankara', 'TR'),
('Bursaray', 'Bursa', 'TR'),
('Istanbul Metro', 'Istanbul', 'TR'),
('Izmir Metro', 'Izmir', 'TR'),
('Dnipro Metro', 'Dnipro', 'UA'),
('Kharkiv Metro', 'Kharkiv', 'UA'),
('Kyiv Metro', 'Kyiv', 'UA'),
('Dubai Metro', 'Dubai', 'AE'),
('Glasgow Subway', 'Glasgow', 'GB'),
('London Underground', 'London', 'GB'),
('Docklands Light Railway', 'London', 'GB'),
('MARTA', 'Atlanta', 'US'),
('Baltimore Metro SubwayLink', 'Baltimore', 'US'),
('MBTA', 'Boston', 'US'),
('Chicago L', 'Chicago', 'US'),
('RTA Rapid Transit', 'Cleveland', 'US'),
('Metro Rail', 'Los Angeles', 'US'),
('Metrorail', 'Miami', 'US'),
('New York City Subway', 'New York City', 'US'),
('SEPTA', 'Philadelphia', 'US'),
('BART', 'San Francisco', 'US'),
('Tren Urbano', 'San Juan', 'US'),
('Washington Metro', 'Washington, D.C.', 'US'),
('Tashkent Metro', 'Tashkent', 'UZ'),
('Caracas Metro', 'Caracas', 'VE');
-- Inner Join
select subway_system.subway_system,
subway_system.city,
country.country
from subway_system
inner join country
on subway_system.country_code = country.country_code;
-- Table Aliasing
select s.subway_system,
s.city,
c.country
from subway_system s
inner join country c
on s.country_code = c.country_code;
-- Table aliasing using the optional "as" keyword
select s.subway_system,
s.city,
c.country
from subway_system as s
inner join country as c
on s.country_code = c.country_code;
-- Inner Join with tables aliasing using "join". The word "inner" is optional.
select s.subway_system,
s.city,
c.country
from subway_system s
join country c
on s.country_code = c.country_code;
-- Right Outer Join
select c.country,
s.city,
s.subway_system
from subway_system s right outer join country c
on s.country_code = c.country_code;
-- Left Outer Join
select c.country,
s.city,
s.subway_system
from country c left outer join subway_system s
on s.country_code = c.country_code;
-- Natural Join
select *
from subway_system s
natural join country c;
-- Building the tables for the the cross join example below
create database restaurant;
use restaurant;
create table main_dish
(
main_item varchar(100)
);
create table side_dish
(
side_item varchar(100)
);
insert into main_dish (main_item)
values
('steak'),
('chicken'),
('ham');
insert into side_dish (side_item)
values
('french fries'),
('rice'),
('potato chips');
-- Cross Join
select m.main_item,
s.side_item
from main_dish m
cross join side_dish s;
-- Create table for the self join example below
create database music_club;
use music_club;
create table music_preference
(
music_fan varchar(100),
favorite_genre varchar(100)
);
insert into music_preference (music_fan, favorite_genre)
values
('Bob', 'Reggae'),
('Earl', 'Bluegrass'),
('Ella', 'Jazz'),
('Peter', 'Reggae'),
('Benny', 'Jazz'),
('Bunny', 'Reggae'),
('Sierra', 'Bluegrass'),
('Billie', 'Jazz');
-- Self Join
select a.music_fan,
b.music_fan
from music_preference a
inner join music_preference b
on (a.favorite_genre = b.favorite_genre)
where a.music_fan != b.music_fan
order by a.music_fan;
-- Switch back to the subway database
use subway;
-- No Parentheses
select s.subway_system,
s.city,
c.country
from subway_system as s
inner join country as c
on s.country_code = c.country_code;
-- With Parentheses
select s.subway_system,
s.city,
c.country
from subway_system as s
inner join country as c
on (s.country_code = c.country_code);
-- Old School Inner Join
select s.subway_system,
s.city,
c.country
from subway_system as s,
country as c
where s.country_code = c.country_code;
-- Column Aliasing
select s.subway_system as metro,
s.city,
c.country
from subway_system as s
inner join country as c
on s.country_code = c.country_code
where c.country_code = 'FR';
-- Specifying the database name
select * from subway.subway_system;
-- Create a copy of the country table in the location database to test joining tables in different databases
create database location;
create table location.country as select * from subway.country;
-- Joining Tables in Different Databases
select s.subway_system,
s.city,
c.country
from subway.subway_system as s
inner join location.country as c
on s.country_code = c.country_code;
-- Try It Yourself Exercises. First, let's create the tables and load the data.
create database solar_system;
use solar_system;
create table planet
(
planet_id int,
planet_name varchar(50)
);
create table ring
(
planet_id int,
ring_tot int
);
insert into planet (planet_id, planet_name)
values
(1, 'Mercury'),
(2, 'Venus'),
(3, 'Earth'),
(4, 'Mars'),
(5, 'Jupiter'),
(6, 'Saturn'),
(7, 'Uranus'),
(8, 'Neptune');
insert into ring (planet_id, ring_tot)
values
(5, 3),
(6, 7),
(7, 13),
(8, 6);
-- Exercise 5-1: Inner Join
select p.planet_name,
r.ring_tot
from planet p
inner join ring r
on p.planet_id = r.planet_id;
-- Exercise 5-2: Left Outer Join
select p.planet_name,
r.ring_tot
from planet p
left join ring r
on p.planet_id = r.planet_id;
-- Exercise 5-3: Right Outer Join
select p.planet_name,
r.ring_tot
from ring r
right join planet p
on p.planet_id = r.planet_id;
-- Exercise 5-4: Column Alias
select p.planet_name,
r.ring_tot as rings
from ring r
right join planet p
on p.planet_id = r.planet_id;