-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasics_1
executable file
·230 lines (158 loc) · 6.98 KB
/
Basics_1
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
-- https://sqlzoo.net/wiki/SELECT_basics
-- 1. Introducing the world table of countries - Modify it to show the population of Germany
select population from world where name = 'Germany'
-- 2. Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.
select name, population from world where name in ('Sweden','Norway','Denmark')
-- 3. Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to show the country and the area for countries with an area between 200,000 and 250,000
SELECT name, area FROM world
where area between 200000 and 250000
-- https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial
-- 1. List each country name where the population is larger than that of 'Russia'
select name
from world where population>(select population from world where name='Russia')
-- 2. Show the names of countries in Europe with a per capita GDP greater than 'United Kingdom'.
select name from world
where continent = 'Europe' and
gdp/population > (select gdp/population from world where name = 'United Kingdom')
-- 3. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.
select name, continent from world
where continent in (select continent from world where name in('Argentina','Australia'))
order by name
-- 4. Which country has a population that is more than Canada but less than Poland? Show the name and the population.
select name, population
from world
where population>(select population from world where name='Canada')
and population<(select population from world where name ='Poland')
-- 5. Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.
select name, CONCAT(ROUND(100*population/(select population from world where name = 'Germany')),'%') as population
from world
where continent = 'Europe'
-- 6. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
select name from world
where gdp > ALL(select gdp from world where continent ='Europe' and gdp>0)
and continent<>'Europe'
-- 7. Find the largest country (by area) in each continent, show the continent, the name and the area
select a.continent, a.name, a.area from world a
where
a.area >= ALL(select x.area from world x where a.continent = x.continent)
-- 8. List each continent and the name of the country that comes first alphabetically.
select a.continent, a.name from world a
where
a.name <= ALL(select b.name from world b where a.continent = b.continent)
-- https://sqlzoo.net/wiki/SUM_and_COUNT
-- 1. Show the total population of the world.
select sum(population)
from world
--2. List all the continents - just once each.
select distinct continent from world
-- 3. Give the total GDP of Africa
select sum(gdp) from world
where continent = 'Africa'
-- 4. How many countries have an area of at least 1000000
select count(name) from world
where area>1000000
-- 5. What is the total population of ('Estonia', 'Latvia', 'Lithuania')
select sum(population) from world where
name in ('Estonia', 'Latvia', 'Lithuania')
-- 6. For each continent show the continent and number of countries
select continent, count(name)
from world
group by(continent)
-- 7. For each continent show the continent and number of countries with populations of at least 10 million.
select continent, count(name)
from world
where population>10000000
group by(continent)
-- 8. List the continents that have a total population of at least 100 million.
select continent from world
group by (continent)
having sum(population) >= 100000000
-- https://sqlzoo.net/wiki/The_JOIN_operation
-- 1. Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'
select a.id, b.player
from game a
join
goal b
on a.id = b.matchid
where b.teamid = 'GER'
-- 2. Notice in the that the column matchid in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table.
Show id, stadium, team1, team2 for just game 1012
select distinct a.id,a.stadium,a.team1,a.team2
from game a
join
goal b
on a.id = b.matchid
where b.matchid= 1012
-- 3. Modify it to show the player, teamid, stadium and mdate for every German goal.
select g.player, g.teamid, ga.stadium, ga.mdate from
goal g
join game ga
on g.matchid = ga.id
where g.teamid = 'GER'
-- 4. Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'
select g.team1, g.team2, go.player
from game g
join goal go
on g.id = go.matchid
where go.player like 'Mario%'
-- 5. Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10
select go.player, go.teamid, e.coach, go.gtime
from goal go
join
eteam e
on go.teamid = e.id
where go.gtime<=10
-- 6. List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.
select g.mdate, e.teamname
from game g
join eteam e
on g.team1 = e.id
where
e.coach = 'Fernando Santos'
-- 7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'
select go.player
from goal go
join
game gg
on go.matchid = gg.id
where gg.stadium = 'National Stadium, Warsaw'
-- 8. Instead show the name of all players who scored a goal against Germany
SELECT DISTINCT player
FROM game
JOIN goal
ON matchid = id
WHERE (teamid!='GER' AND (team1='GER' OR team2='GER'))
--Show teamname and the total number of goals scored.
select e.teamname, count(go.gtime)
from eteam e
join goal go
on e.id = go.teamid
group by(e.teamname)
-- 10. Show the stadium and the number of goals scored in each stadium.
select g.stadium, count(go.gtime)
from game g
join goal go
on g.id = go.matchid
group by(g.stadium)
-- 11. For every match involving 'POL', show the matchid, date and the number of goals scored.
select g.id,g.mdate, count(go.gtime)
from game g
join goal go
on g.id = go.matchid
where (g.team1='POL' or g.team2='POL')
group by g.mdate,g.id
-- 12. For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
select g.id, g.mdate, count(go.gtime)
from game g
join goal go
on g.id = go.matchid
where go.teamid = 'GER'
group by g.id, g.mdate
-- 13. List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.
SELECT mdate,
team1,
SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1,
team2,
SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2
FROM game LEFT JOIN goal ON matchid = id
GROUP BY mdate,matchid,team1,team2