SQLZOO习题详解(07)——The JOIN operation
Posted 谷哥的小弟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLZOO习题详解(07)——The JOIN operation相关的知识,希望对你有一定的参考价值。
版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
表及其关系
game表如下:
goal表如下:
eteam表如下:
表的关系如下:
链接地址
https://napier.sqlzoo.net/wiki/The_JOIN_operation
SQL练习
01、请查询出德国GER队进球球员的名字以及对应比赛的matchid
SELECT matchid ,player FROM goal WHERE teamid = 'GER'
02、请查询id为1012的比赛的id,stadium,team1,team2
SELECT id,stadium,team1,team2 FROM game where id=1012
03、请查询德国队GER进球球员的球员名,队伍名,比赛场馆,比赛日期
SELECT player,teamid,stadium,mdate FROM game INNER JOIN goal ON (id=matchid) WHERE teamid='GER'
SELECT player,teamid,stadium,mdate FROM game INNER JOIN goal ON (game.id=goal.matchid) WHERE goal.teamid='GER'
04、请查询进球球员名字以Mario开头的相关信息:比赛双方的球队名以及球员名
SELECT team1,team2, player FROM game INNER JOIN goal ON (game.id=goal.matchid) WHERE goal.player LIKE 'Mario%'
05、请查询比赛中前10分钟有进球的比赛的相关信息:进球球员名字player,球队id即teamid,教练名字coach,进球时间gtime
SELECT player, teamid, coach, gtime
FROM goal INNER JOIN eteam ON goal.teamid = eteam.id
WHERE gtime <= 10
06、请查询Fernando Santos作为队伍team1的教练的赛事日期和球队名
SELECT mdate, teamname
FROM game INNER JOIN eteam ON game.team1 = eteam.id
WHERE eteam.coach = 'Fernando Santos'
07、请查询在National Stadium, Warsaw球馆进球的球员的名字
SELECT player
FROM game INNER JOIN goal ON (game.id = goal.matchid)
WHERE stadium = 'National Stadium, Warsaw'
08、查询其他球队在于德国队的比赛中进球的球员名字
SELECT DISTINCT(player)
FROM game INNER JOIN goal ON (game.id = goal.matchid)
WHERE teamid!='GER' AND (team1 = 'GER' OR team2 = 'GER')
09、请查询各球队的名称及其进球总数
SELECT teamname, COUNT(player)
FROM eteam INNER JOIN goal ON eteam.id = goal.teamid
GROUP BY teamname
10、请查询各体育场的名称以及在该体育场的进球数
SELECT stadium, COUNT(*)
FROM game JOIN goal ON game.id=goal.matchid
GROUP BY stadium
11、请查询波兰POL参与的每场比赛的比赛日期和进球数
SELECT id, mdate, COUNT(*)
FROM game INNER JOIN goal ON goal.matchid = game.id
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY id, mdate
12、查询德国GER队参与的比赛的相关信息:比赛编号matchid,比赛日期mdate,以及德国队的进球数
SELECT matchid, mdate, COUNT(*)
FROM game INNER JOIN goal ON (game.id= goal.matchid)
WHERE (game.team1 = 'GER' OR game.team2 = 'GER') AND teamid = 'GER'
GROUP BY matchid, 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.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 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
以上是关于SQLZOO习题详解(07)——The JOIN operation的主要内容,如果未能解决你的问题,请参考以下文章
SQLZOO习题详解(08)——The JOIN operation
SQL练习-SQLZOO SQLZOO:The JOIN operation
SQL练习-SQLZOO SQLZOO:The JOIN operation