-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_all_problems.sql
277 lines (257 loc) · 6.62 KB
/
sql_all_problems.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
CREATE SCHEMA euro_cup_2016;
USE euro_cup_2016;
/*
* 1. Write a SQL query to find the date EURO Cup 2016 started on.
*/
select
min(play_date) as play_start_date
from match_mast;
/*
* 2. Write a SQL query to find the number of matches that were won by penalty shootout.
*/
select
count(distinct match_no) as match_count
from match_mast
where decided_by = 'P';
/*
* 3. Write a SQL query to find the match number, date, and score for matches in which
* no stoppage time was added in the 1st half.
*/
select
match_no
, play_date as match_date
, goal_score
from match_mast
where stop1_sec = 0;
/*
* 4. Write a SQL query to compute a list showing the number of substitutions that
* happened in various stages of play for the entire tournament.
*/
select
play_stage
, sum(case when in_out = 'I' then 1 end) as num_substitutions
from match_mast mm
inner join player_in_out pio
on mm.match_no = pio.match_no
group by play_stage
order by play_stage;
/*
* 5. Write a SQL query to find the number of bookings that happened in stoppage time.
*/
select
count(*) as ST_booking_count
from player_booked
where play_schedule = 'ST';
/*
* 6. Write a SQL query to find the number of matches that were won by a single point, but
* do not include matches decided by penalty shootout.
*/
select
count(distinct match_no) as matchcount
from match_mast
where abs(cast(substring(goal_score, 1, locate('-', goal_score) - 1) as signed) -
cast(substring(goal_score, locate('-', goal_score) + 1, length(goal_score)) as signed)) = 1
and decided_by = 'N';
/*
* 7. Write a SQL query to find all the venues where matches with penalty shootouts were
* played.
*/
select
sv.venue_id
, sv.venue_name
from match_mast mm
inner join soccer_venue sv
on mm.venue_id = sv.venue_id
where decided_by = 'P';
/*
* 8. Write a SQL query to find the match number for the game with the highest number of
* penalty shots, and which countries played that match.
*/
select
match_no
, team_id
, count(distinct kick_id) as penalty_kick_count
from penalty_shootout
group by
match_no
, team_id
order by
penalty_kick_count desc
limit 2;
/*
* 9. Write a SQL query to find the goalkeeper’s name and jersey number, playing for
* Germany, who played in Germany’s group stage matches
*/
select distinct
player_name
, jersey_no
from soccer_team st
inner join soccer_country sc
on st.team_id = sc.country_id
inner join player_mast pm
on st.team_id = pm.team_id
inner join match_details md
on st.team_id = md.team_id
where sc.country_name = 'Germany'
and pm.posi_to_play = 'GK'
and md.play_stage = 'G';
/*
* 10. Write a SQL query to find all available information about the players under contract to
* Liverpool F.C. playing for England in EURO Cup 2016.
*/
select
pm.*
from player_mast pm
inner join soccer_country sc
on pm.team_id = sc.country_id
where pm.playing_club = 'Liverpool'
and sc.country_name = 'England';
/*
* 11. Write a SQL query to find the players, their jersey number, and playing club who
* were the goalkeepers for England in EURO Cup 2016.
*/
select
pm.player_id
, pm.player_name
, pm.jersey_no
, pm.playing_club
from player_mast pm
inner join soccer_country sc
on pm.team_id = sc.country_id
and sc.country_name = 'England'
where pm.posi_to_play = 'GK';
/*
* 12. Write a SQL query that returns the total number of goals scored by each position on
* each country’s team. Do not include positions which scored no goals.
*/
select
sc.country_name
, pm.posi_to_play
, count(distinct gd.goal_id) as goal_count
from goal_details gd
inner join player_mast pm
on gd.player_id = pm.player_id
inner join soccer_country sc
on gd.team_id = sc.country_id
group by
sc.country_name
, pm.posi_to_play
order by
sc.country_name
, goal_count desc;
/*
* 13. Write a SQL query to find all the defenders who scored a goal for their teams.
*/
select
pm.player_id
, pm.player_name
, pm.posi_to_play
, count(distinct gd.goal_id) as goal_count
from goal_details gd
inner join player_mast pm
on gd.player_id = pm.player_id
where pm.posi_to_play = 'DF'
group by
pm.player_id
, pm.player_name
order by goal_count desc;
/*
* 14. Write a SQL query to find referees and the number of bookings they made for the
* entire tournament. Sort your answer by the number of bookings in descending order.
*/
select
rm.referee_id
, rm.referee_name
, count(*) as booking_count
from referee_mast rm
inner join match_mast mm
on rm.referee_id = mm.referee_id
inner join player_booked pb
on mm.match_no = pb.match_no
group by
rm.referee_id
, rm.referee_name
order by booking_count desc;
/*
* 15. Write a SQL query to find the referees who booked the most number of players.
*/
select
rm.referee_id
, rm.referee_name
, count(distinct pb.player_id) as booking_player_count
from referee_mast rm
inner join match_mast mm
on rm.referee_id = mm.referee_id
inner join player_booked pb
on mm.match_no = pb.match_no
group by
rm.referee_id
, rm.referee_name
order by booking_player_count desc;
/*
* 16. Write a SQL query to find referees and the number of matches they worked in each
* venue.
*/
select
rm.referee_id
, rm.referee_name
, sv.venue_name
, count(distinct mm.match_no) as match_count
from referee_mast rm
inner join match_mast mm
on rm.referee_id = mm.referee_id
inner join soccer_venue sv
on mm.venue_id = sv.venue_id
group by
rm.referee_id
, rm.referee_name
, sv.venue_name
order by match_count desc;
/*
* 17. Write a SQL query to find the country where the most assistant referees come from,
* and the count of the assistant referees.
*/
select
sc.country_name
, count(distinct arm.ass_ref_id) as asst_ref_count
from asst_referee_mast arm
inner join soccer_country sc
on arm.country_id = sc.country_id
group by
sc.country_name
order by asst_ref_count desc;
/*
* 18. Write a SQL query to find the highest number of foul cards given in one match.
*/
select
pb.match_no
, count(*) as card_count
from player_booked pb
group by
pb.match_no
order by card_count desc;
/*
* 19. Write a SQL query to find the number of captains who were also goalkeepers.
*/
select distinct
pm.player_id
, pm.player_name
from match_details md
inner join match_captain mc
on md.match_no = mc.match_no
and md.player_gk = mc.player_captain
inner join player_mast pm
on mc.player_captain = pm.player_id;
/*
* 20. Write a SQL query to find the substitute players who came into the field in the first
* half of play, within a normal play schedule.
*/
select
pm.player_id
, pm.player_name
from player_in_out pio
inner join player_mast pm
on pio.player_id = pm.player_id
where pio.in_out = 'I'
and pio.play_schedule = 'NT'
and pio.play_half = 1;