美文网首页
SQL练习_4 | 6 | SQLZOO_20191012

SQL练习_4 | 6 | SQLZOO_20191012

作者: XDuan | 来源:发表于2019-10-23 20:57 被阅读0次

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

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

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

目录
6 Join

6 Join

查询表格

查询表格_表格间关系 查询表格_Game & Goal 查询表格_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.

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

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

  • SQL练习

    SQL练习-4张表 针对下面的4张表格进行SQL语句的练习。 image SQL练习-题目 查询001课程比002...

  • 牛客网SQL实战练习——6~10

    牛客网SQL实战练习——6~10 声明:练习牛客网SQL实战题目,整理笔记。6.查找所有员工入职时候的薪水情况,给...

  • sql 练习(五)

    环境是mysql 练习数据见SQL:练习的前期准备 sql 练习(一)sql 练习(二)sql 练习(三)sql ...

  • sql 练习(四)

    环境是mysql 练习数据见SQL:练习的前期准备 sql 练习(一)sql 练习(二)sql 练习(三)31、查...

  • sql 练习(三)

    环境是mysql 练习数据见SQL:练习的前期准备 sql 练习(一)sql 练习(二)21、查询成绩高于学号为“...

  • 2019-08-03简单SQL注入的手工测试

    SQL注入在线练习平台(http://leettime.net) 练习基础模块4 1、判断闭合字符和列数 http...

  • Mysql防SQL注入

    原文地址:https://www.jianshu.com/p/ce6c51356b4e SQL注入 SQL注入是一...

  • MySQL Operation

    sql语句练习sql练习2 MYSQL导入数据出现The MySQL server is running with...

  • SQL经典练习(1~6)

    今天开始作息下SQL相关的练习题,题目来源这里 首先是数据库的准备,按照题目要求,自己创建了名为text_dat...

网友评论

      本文标题:SQL练习_4 | 6 | SQLZOO_20191012

      本文链接:https://www.haomeiwen.com/subject/ujnkvctx.html