-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-JOIN-operation.sql
More file actions
351 lines (271 loc) · 7.15 KB
/
Copy path06-JOIN-operation.sql
File metadata and controls
351 lines (271 loc) · 7.15 KB
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
/*
This tutorial introduces JOIN which allows you to use data from two or more tables.
https://sqlzoo.net/wiki/The_JOIN_operation
understanding different types of joins:
https://www.youtube.com/watch?v=zGSv0VaOtR0&ab_channel=JomaClass
*/
--#1
/*
Show the matchid and player name for all goals scored by Germany.
*/
SELECT matchid, player FROM goal
WHERE teamid = 'GER'
--#2
/*
Show id, stadium, team1, team2 for just game 1012
*/
SELECT team1, team2 FROM game
WHERE id = 1012
--#3
/*
Show the player, teamid, stadium and mdate for every German goal
*/
SELECT player, teamid, stadium, mdate
FROM game game JOIN goal goal ON (game.id = goal.matchid)
WHERE teamid = 'GER'
--#4
/*
Show the team1, team2 and player for every goal scored by a player called Mario.
*/
SELECT team1, team2, player
FROM game JOIN goal ON (id = matchid)
WHERE player LIKE 'Mario%'
--#5
/*
Show player, teamid, coach, gtime for all goals scored in the first 10 minute
*/
SELECT player, teamid, coach, gtime
FROM goal JOIN eteam ON teamid = id
WHERE gtime <= 10
--#6
/*
List the dates of the matches and the name of the team
in which 'Fernando Santos' was the team1 coach.
*/
SELECT mdate, teamname
FROM eteam JOIN game ON eteam.id = game.team1
WHERE coach = 'Fernando Santos'
--#7
/*
List the player for every goal scored in a game
where the stadium was 'National Stadium, Warsaw'
*/
SELECT player
FROM goal JOIN game ON matchid = id
WHERE stadium = 'National Stadium, Warsaw'
--#8
/*
Show the name of all players who scored a goal against Germany.
*/
SELECT DISTINCT player
FROM game JOIN goal ON matchid = id
WHERE
'GER' IN (team1, team2) AND
teamid <> 'GER'
--#9
/*
Show teamname and the total number of goals scored.
*/
SELECT teamname, COUNT(*) AS goals
FROM eteam JOIN goal ON teamid = id
GROUP BY teamname
--#10
/*
Show the stadium and the number of goals scored in each stadium.
*/
SELECT stadium, COUNT(*) AS goals
FROM game JOIN goal ON matchid = id
GROUP BY stadium
--#11
/*
For every match involving 'POL', show the matchid, date and the number of goals scored.
*/
SELECT matchid, mdate, COUNT(*) AS goals
FROM game JOIN goal ON matchid = id
WHERE 'POl' IN (team1, team2)
GROUP BY matchid
--#12
/*
For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
*/
SELECT matchid, mdate, COUNT(*) AS 'goals by germany'
FROM game JOIN goal ON (matchid = id AND teamid = 'GER')
GROUP BY matchid
--#13
/*
List every match with the goals scored by each team as shown:
mdate | team1 | score1 | team2 | score2
--------------|-------|--------|-------|-------
1 July 2012 | ESP | 4 | ITA | 0
10 June 2012 | ESP | 1 | ITA | 1
10 June 2012 | IRL | 1 | CRO | 3
Sort your result by mdate, matchid, team1 and team2
CASE WHEN: https://sqlzoo.net/wiki/CASE
*/
SELECT
mdate,
team1,
SUM(CASE WHEN teamid = team1 THEN 1 ELSE 0 END) AS score1,
team2,
SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) AS score2
FROM game LEFT JOIN goal ON matchid = id
GROUP BY id
ORDER BY mdate, matchid, team1, team2
--#14
/*
question: https://leetcode.com/problems/game-play-analysis-iv/
*/
SELECT ROUND(1. * COUNT(*) / (SELECT COUNT(DISTINCT player_id) FROM activity), 2) AS fraction
FROM activity
WHERE (player_id, event_date) IN (
SELECT
player_id, (MIN(event_date) + INTERVAL '1 day') AS next_day
FROM activity
GROUP BY player_id
)
--#15
/*
question: https://leetcode.com/problems/trips-and-users/
*/
SELECT
request_at AS "Day",
ROUND(1. * SUM(CASE WHEN status <> 'completed' THEN 1 ELSE 0 END) /
COUNT(*),
2) AS "Cancellation Rate"
FROM trips
WHERE
'No' = ALL (SELECT banned FROM users WHERE users_id IN (client_id, driver_id)) AND
request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY "Day"
-- another solution
SELECT
request_at AS "Day",
ROUND(1. * SUM(CASE WHEN status <> 'completed' THEN 1 ELSE 0 END) /
COUNT(*),
2) AS "Cancellation Rate"
FROM trips
JOIN users client ON client.users_id = trips.client_id
JOIN users driver ON driver.users_id = trips.driver_id
WHERE
request_at BETWEEN '2013-10-01' AND '2013-10-03' AND
client.banned = 'No' AND driver.banned = 'No'
GROUP BY "Day"
/*
More JOIN operations
https://sqlzoo.net/wiki/More_JOIN_operations
*/
--#6
/*
Obtain the cast list (the names of the actors who were in the movie) for 'Casablanca'.
*/
SELECT name FROM casting
JOIN actor ON actorid = id
WHERE movieid = 11768
--#7
/*
Obtain the cast list for the film 'Alien'
*/
SELECT name
FROM casting
JOIN actor ON actor.id = actorid
WHERE movieid IN (
SELECT id FROM movie WHERE title = 'Alien'
)
--#8
/*
List the films in which 'Harrison Ford' has appeared
*/
SELECT title
FROM movie
JOIN casting ON movieid = movie.id
WHERE 'Harrison Ford' = (
SELECT name FROM actor WHERE actor.id = actorid
)
--#9
/*
List the films where 'Harrison Ford' has appeared - but not in the starring role.
[Note: the ord field of casting gives the position of the actor.
If ord=1 then this actor is in the starring role]
*/
SELECT title
FROM movie
JOIN casting ON movieid = movie.id AND ord != 1
JOIN actor ON actorid = actor.id AND name = 'Harrison Ford'
--#10
/*
List the films together with the leading star for all 1962 films.
*/
SELECT title, name AS "leading star"
FROM movie
JOIN casting ON movieid = movie.id AND ord = 1
JOIN actor ON actorid = actor.id
WHERE yr = 1962
--#11
/*
Which were the busiest years for 'Rock Hudson', show the year and the number of movies he made each year
for any year in which he made more than 2 movies.
*/
SELECT yr, COUNT(*) AS "movies"
FROM movie
JOIN casting ON movieid = movie.id
JOIN actor ON actorid = actor.id AND name = 'Rock Hudson'
GROUP BY yr
HAVING movies > 2
--#12
/*
List the film title and the leading actor for all of the films 'Julie Andrews' played in.
*/
SELECT title, name
FROM movie m1
JOIN casting c1 ON c1.movieid = m1.id
JOIN actor a1 ON c1.actorid = a1.id
WHERE
(
SELECT COUNT(*)
FROM movie m2
JOIN casting c2 ON c2.movieid = m2.id
JOIN actor a2 ON c2.actorid = a2.id AND a2.name = 'Julie Andrews'
WHERE m2.id = m1.id
) > 0 AND
ord = 1
--#13
/*
Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
*/
SELECT name, COUNT(*)
FROM actor
JOIN casting ON actorid = id AND ord = 1
GROUP BY actor.id
HAVING COUNT(*) >= 15
ORDER BY name
--#14
/*
List the films released in the year 1978 ordered by
the number of actors in the cast, then by title.
*/
SELECT title, COUNT(*)
FROM movie
JOIN casting ON movieid = id
WHERE yr = 1978
GROUP BY id
ORDER BY COUNT(*) DESC, title
--#15
/*
List all the people who have worked with 'Art Garfunkel'.
*/
SELECT name FROM actor x
WHERE (
-- where x worked with 'Art Garfunkel'
SELECT COUNT(*) FROM casting c1
JOIN actor ON actor.id = actorid AND actor.name = 'Art Garfunkel'
JOIN casting c2 ON c1.movieid = c2.movieid AND c2.actorid = x.id
LIMIT 1
) = 1 AND
x.name <> 'Art Garfunkel'
SELECT DISTINCT name FROM actor
JOIN casting ON id = actorid
WHERE movieid IN (
SELECT movieid FROM casting
JOIN actor ON id = actorid
WHERE name = 'Art Garfunkel'
) AND name <> 'Art Garfunkel'