SQL ZOO PART 2 : SUM and COUNT & The JOIN operation

承接上一篇 的練習,
這次繼續玩下去~~~

SUM and COUNT

https://sqlzoo.net/wiki/SUM_and_COUNT


1. Show the total population of the world.

SELECT SUM(population)
FROM world

2. List all the continents - just once each.

SELECT DISTINCT(continent)
FROM world

3. Give the total GDP of Africa

SELECT SUM(gdp) from world
where continent like 'Africa'

4. How many countries have an area of at least 1000000

SELECT COUNT(name) FROM world
WHERE area > 1000000

5. What is the total population of ('Estonia', 'Latvia', 'Lithuania')


SELECT SUM(population) FROM world
where name in ('Estonia', 'Latvia', 'Lithuania')

6. For each continent show the continent and number of countries.

SELECT continent, COUNT(name) FROM world
GROUP BY continent

7. For each continent show the continent and number of countries with populations of at least 10 million.

SELECT continent, COUNT(name) FROM world
WHERE population >= 10000000
GROUP BY continent

8. List the continents that have a total population of at least 100 million.

SELECT continent
FROM world
group by continent
having sum(population)>100000000

The JOIN operation



這是在JOIN練習中會用到的三個資料集,會使用JOIN的方式將資料集連結在一起,圖中連結的線是他們共有的Key,可以參照以前寫的文章 了解詳細


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 LIKE 'GER'

2. Show id, stadium, team1, team2 for just game 1012

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

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

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

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 JOIN goal ON (id=matchid)
  WHERE goal.player LIKE 'Mario%';

5. Show playerteamidcoachgtime for all goals scored in the first 10 minutes gtime<=10

SELECT player, teamid,coach, gtime
FROM goal JOIN eteam on teamid=id 
 WHERE gtime<=10

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

SELECT game.mdate,
       eteam.teamname
    FROM game
    JOIN eteam ON game.team1 = eteam.id
    WHERE eteam.coach = 'Fernando Santos';

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

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


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

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

9. Show teamname and the total number of goals scored.

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

10. Show the stadium and the number of goals scored in each stadium.

 SELECT stadium, COUNT(teamid)
 FROM game JOIN goal ON (id=matchid)
GROUP BY stadium


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

SELECT matchid, mdate, COUNT(player) AS goals
FROM game
JOIN goal ON (matchid=id AND (team1 = 'POL' OR team2 = 'POL'))
GROUP BY matchid, mdate

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

SELECT matchid, mdate, COUNT(teamid) 
FROM game
JOIN goal ON (id=matchid AND (team1 = 'GER' OR team2 = 'GER') AND teamid='GER')
GROUP BY id, 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.
mdateteam1score1team2score2
1 July 2012ESP4ITA0
10 June 2012ESP1ITA1
10 June 2012IRL1CRO3
...
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) AS score1,
       team2,
       SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) AS score2 FROM
    game LEFT JOIN goal ON (id = matchid)
    GROUP BY mdate,team1,team2
    ORDER BY mdate, matchid, team1, team2

留言

這個網誌中的熱門文章

Word Vector & Word embedding 初探 - with n-Gram & GLOVE Model

文字探勘之關鍵字萃取 : TF-IDF , text-rank , RAKE

多元迴歸分析- subsets and shrinkage