본문 바로가기
공부/SQL

SQL Zoo 07 - JOIN

by 혼밥맨 2022. 1. 11.
반응형

SQL Zoo 07 - JOIN


Database


문제 1.  The first example shows the goal scored by a player with the last name 'Bender'. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime. 
Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'
정답
1
2
3
SELECT matchid, player
FROM goal
WHERE teamid='GER'
cs
 
 
문제 2.  From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match. 

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
정답
1
2
3
SELECT id, stadium, team1, team2
FROM game
WHERE id = 1012
cs

 

 

문제 3. 

정답
1
2
3
SELECT player,teamid,stadium, mdate
FROM game JOIN goal ON (id=matchid)
WHERE teamid = 'GER'
cs

 

 

문제 4.

Use the same JOIN as in the previous question.

Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

정답
1
2
3
SELECT team1, team2, player
FROM game JOIN goal ON (id = matchid)
WHERE player LIKE 'Mario%';
cs

 

 

문제 5.

The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id

Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

정답
1
2
3
SELECT player, teamid, coach, gtime
FROM goal JOIN eteam on (teamid=id)
WHERE gtime<=10
cs

 

 

문제 6.
정답
1
2
3
SELECT mdate, teamname
FROM eteam JOIN game ON (team1 = eteam.id)
WHERE coach = 'Fernando Santos'
cs

 

 

문제 7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'
정답
1
2
3
SELECT player
FROM game JOIN goal ON (id=matchid)
WHERE stadium = 'National Stadium, Warsaw';
cs

 

문제 8. 
The example query shows all goals scored in the Germany-Greece quarterfinal.

Instead show the name of all players who scored a goal against Germany.

정답
1
2
3
SELECT DISTINCT player
FROM goal JOIN game ON matchid = id
WHERE teamid != 'GER' AND (team1 = 'GER' OR team2 = 'GER');
cs

 

 

문제 9.
Show teamname and the total number of goals scored.
COUNT and GROUP BY
정답
1
2
3
SELECT teamname, COUNT(matchid)
FROM eteam JOIN goal ON id=teamid
GROUP BY teamname
cs

 

 

문제 10.
Show teamname and the total number of goals scored.
COUNT and GROUP BY
정답
1
2
3
4
5
SELECT stadium, COUNT(matchid)
 
FROM game JOIN goal ON id = matchid
 
GROUP BY stadium;
cs

 

 

문제 11.
For every match involving 'POL', show the matchid, date and the number of goals scored.
정답
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT id, mdate, num
 
FROM (SELECT matchid as id, count(teamid) as num
 
      FROM goal
 
      GROUP BY matchid)A
 
NATURAL JOIN
 
     (SELECT id, mdate
 
      FROM game
 
      WHERE (team1 = 'POL' OR team2 = 'POL'))B;
cs

 

 

문제 12.
For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
정답
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT *
 
FROM (SELECT id, mdate
 
      FROM game)A
 
NATURAL JOIN
 
     (SELECT matchid AS id, count(teamid)
 
      FROM goal
 
      WHERE teamid = 'GER'
 
      GROUP BY matchid)B;
cs

 

 

문제 13.
정답
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
select mdate, team1, sum(score1) as score1, team2, sum(score2) as score2
 
from
 
(SELECT mdate, matchid,
 
  team1,
 
  CASE WHEN teamid = team1 THEN 1 ELSE 0 END score1,
 
  team2,
 
  CASE WHEN teamid = team2 THEN 1 ELSE 0 END score2
 
  FROM game LEFT JOIN goal ON matchid = id) t
 
group by matchid, mdate
 
order by mdate, matchid, team1, team2
cs

반응형

'공부 > SQL' 카테고리의 다른 글

SQL 09 – Numeric Examples  (0) 2022.01.13
SQL 08 – More JOIN operations  (0) 2022.01.13
SQL Zoo 06 - SUM and COUNT  (0) 2022.01.11
SQL Zoo 05 - SELECT within SELECT Tutorial  (0) 2022.01.09
SQL Zoo 04 - SELECT from Nobel Tutorial  (0) 2022.01.09

댓글