-
Notifications
You must be signed in to change notification settings - Fork 0
/
6c_JOIN_old_join_tutorial.sql
66 lines (47 loc) · 1.56 KB
/
6c_JOIN_old_join_tutorial.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
-- https://sqlzoo.net/wiki/Old_JOIN_Tutorial
-- 1. Show the athelete (who) and the country name for medal winners in 2000.
SELECT who,
country.name
FROM ttms
JOIN country ON (ttms.country=country.id)
WHERE games = 2000
-- 2. Show the who and the color of the medal for the medal winners from 'Sweden'.
SELECT who,
color
FROM ttms
JOIN country ON ttms.country = country.id WHERE country.name = 'Sweden'
-- 3. Show the years in which 'China' won a 'gold' medal.
SELECT ttms.games
FROM ttms
JOIN country ON ttms.country = country.id WHERE country.name = 'China'
AND ttms.color = 'gold'
-- 4. Show who won medals in the 'Barcelona' games.
-- Note we use ttws, not ttms
SELECT who
FROM ttws
JOIN games ON ttws.games = games.yr WHERE city = 'Barcelona'
-- 5. Show which city 'Jing Chen' won medals. Show the city and the medal color.
SELECT city,
color
FROM games
JOIN ttws ON ttws.games = games.yr WHERE who = 'Jing Chen';
-- 6. Show who won the gold medal and the city.
SELECT ttws.who,
games.city
FROM games
JOIN ttws ON games.yr = ttws.games
WHERE ttws.color = 'gold'
-- 7. Show the games and color of the medal won by the team that includes 'Yan Sen'.
SELECT ttmd.games,
ttmd.color
FROM ttmd
JOIN team ON ttmd.team = team.id WHERE team.name = 'Yan Sen'
-- 8. Show the 'gold' medal winners in 2004.
SELECT team.name
FROM ttmd
JOIN team ON ttmd.team = team.id WHERE ttmd.color = 'gold'
AND ttmd.games = '2004'
-- 9. Show the name of each medal winner country 'FRA'.
SELECT team.name
FROM ttmd
JOIN team ON ttmd.team = team.id WHERE ttmd.country = 'FRA'