SQL练习_4 | 6 | SQLZOO_20191012

本系列刷题笔记主要用以记录刷SQLZOO的过程中的思路、个人答案以及陌生的或者新的知识点。

题目来源 - SQLZOO
SQLZOO中题目中文版本与英文版本略有差异,题目以英文版为准

相关文章
SQL练习_1 | SQLZOO_20191002
SQL练习_2 | SQLZOO_20191008
SQL练习_3 | SQLZOO_20191010

目录
6 Join

6 Join

查询表格

SQL练习_4 | 6 | SQLZOO_20191012_第1张图片
查询表格_表格间关系
SQL练习_4 | 6 | SQLZOO_20191012_第2张图片
查询表格_Game & Goal
SQL练习_4 | 6 | SQLZOO_20191012_第3张图片
查询表格_eteam

6_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 matchid,
       player
FROM goal
WHERE teamid = 'GER'

6_2 Show id, stadium, team1, team2 for just game 1012

SELECT id,
       stadium,
       team1,
       team2
FROM game
WHERE id = '1012'

6_3 Modify it to show the player, teamid, stadium and mdate for every German goal.

SELECT player,
       teamid,
       stadium,
       mdate
FROM game a
  JOIN goal b ON (id = matchid)
WHERE teamid = 'GER'

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

SELECT team1,
       team2,
       player
FROM game a
  JOIN goal b ON (a.id = b.matchid)
WHERE b.player LIKE 'Mario%'

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

SELECT player,
       teamid,
       coach,
       gtime
FROM goal a
  JOIN eteam b ON a.teamid = b.id
WHERE gtime <= 10

6_6 List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.

SELECT a.mdate,
       b.teamname
FROM game a
  JOIN eteam b ON (a.team1 = b.id)
WHERE b.coach = 'Fernando Santos'

6_7 List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'.

SELECT player
FROM goal a
  JOIN game b ON a.matchid = b.id
WHERE b.stadium = 'National Stadium, Warsaw'

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

SELECT DISTINCT b.player
FROM game a
  JOIN goal b ON a.id = b.matchid
WHERE (b.teamid <> 'GER' AND (a.team1 = 'GER' OR a.team2 = 'GER'))

6_9 Show teamname and the total number of goals scored.

SELECT teamname,
       COUNT(*)
FROM eteam a
  JOIN goal b ON a.id = b.teamid
GROUP BY teamname

6_10 Show the stadium and the number of goals scored in each stadium.

SELECT a.stadium,
       COUNT(b.player)
FROM game a
  JOIN goal b ON a.id = b.matchid
GROUP BY a.stadium

6_11 For every match involving 'POL', show the matchid, date and the number of goals scored.

SELECT matchid,
       mdate,
       COUNT(b.player)
FROM game a
  JOIN goal b ON a.id = b.matchid
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid,
         mdate

6_12 For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'

SELECT b.matchid,
       a.mdate,
       COUNT(b.player)
FROM game a
  JOIN goal b ON a.id = b.matchid
WHERE b.teamid = 'GER'
GROUP BY b.matchid,
         a.mdate

6_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.

SQL练习_4 | 6 | SQLZOO_20191012_第4张图片
examle

Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.

SELECT a.mdate,
       a.team1,
       SUM(CASE WHEN teamid = a.team1 THEN 1 ELSE 0 END) AS score1,
       team2,
       SUM(CASE WHEN teamid = a.team2 THEN 1 ELSE 0 END) AS score2
FROM game a
  LEFT JOIN goal b ON (a.id = b.matchid)
GROUP BY a.mdate,
         b.matchid,
         a.team1,
         a.team2

你可能感兴趣的:(SQL练习_4 | 6 | SQLZOO_20191012)