SQL練習_4 | 6 | SQLZOO_20191012

本系列刷題筆記主要用以記錄刷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
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市留美,隨后出現(xiàn)的幾起案子拇颅,更是在濱河造成了極大的恐慌奏司,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件樟插,死亡現(xiàn)場離奇詭異韵洋,居然都是意外死亡,警方通過查閱死者的電腦和手機黄锤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門搪缨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鸵熟,你說我怎么就攤上這事副编。” “怎么了流强?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵痹届,是天一觀的道長。 經(jīng)常有香客問我打月,道長队腐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任奏篙,我火速辦了婚禮香到,結果婚禮上,老公的妹妹穿的比我還像新娘报破。我一直安慰自己悠就,他們只是感情好,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布充易。 她就那樣靜靜地躺著梗脾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪盹靴。 梳的紋絲不亂的頭發(fā)上炸茧,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音稿静,去河邊找鬼梭冠。 笑死,一個胖子當著我的面吹牛改备,可吹牛的內(nèi)容都是我干的控漠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼盐捷!你這毒婦竟也來了偶翅?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤碉渡,失蹤者是張志新(化名)和其女友劉穎聚谁,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體滞诺,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡形导,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了习霹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片骤宣。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖序愚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情等限,我是刑警寧澤爸吮,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站望门,受9級特大地震影響形娇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜筹误,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一桐早、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧厨剪,春花似錦哄酝、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至直晨,卻和暖如春搀军,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背勇皇。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工罩句, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人敛摘。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓门烂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親兄淫。 傳聞我的和親對象是個殘疾皇子诅福,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容