1. 程式人生 > >sql線上練習網站(http://sqlzoo.cn)答案解析

sql線上練習網站(http://sqlzoo.cn)答案解析

一:SELECThttp://www.sqlzoo.cn/1.htm
    1a. 檢視關於bbc表的詳細說明
        SELECT name, region, population FROM bbc
    2a. 給出人口不小於2億的國家的名稱.(2億是200000000,有8個0)
        SELECT name FROM bbc WHERE population>= 200000000
    2b. 給出人口不小於2億的國家的名稱和人均 GDP
        SELECT name, gdp/population FROM bbc WHERE population >=200000000
    2c. 給出地區屬於'South Asia'的國家的名稱和人口數(以百萬為單位,人口數除以1000000,然後用round函式取整)
        SELECT name, round(population/1000000,0) FROM bbc WHERE region = 'South Asia'
    2d. 給出'France', 'Germany', 'Italy'三個國家的名稱和人口
        SELECT name, population FROM bbc WHERE name in ('France', 'Germany', 'Italy')
    2e. 給出包含'United'字元國家名稱
        SELECT name FROM bbc WHERE name LIKE '%United%'

二:SELECT within SELECThttp://www.sqlzoo.cn/1a.htm
    1a. 給出人口多於Russia(俄國)的國家名稱
        SELECT name FROM bbc  WHERE population > (SELECT population FROM bbc WHERE name='Russia')
    1b. 給出'India'(印度), 'Iran'(伊朗)所在地區的所有國家的所有資訊
        SELECT * FROM bbc WHERE region IN (SELECT region FROM bbc WHERE name IN ('India','Iran'))
    1c. 給出人均GDP超過'United Kingdom'(英國)的歐洲國家
        SELECT name FROM bbc WHERE region = 'Europe' AND gdp/population > (SELECT gdp/population FROM bbc WHERE name = 'United Kingdom')
    1d. 哪個國家的人口比Canada(加拿大)多但少於Algeria(阿爾及利亞)?
        SELECT name FROM bbc WHERE population > (SELECT population FROM bbc WHERE name = 'Canada') AND population < (SELECT population FROM bbc WHERE name = 'Algeria')
    2a. 給出GDP比任何歐洲國家都多的國家(只要顯示國家名稱)
        SELECT name FROM bbc WHERE gdp > ALL(SELECT gdp FROM bbc WHERE region = 'Europe')
    3a. 給出每個地區人口最大的國家
        SELECT name FROM bbc x  WHERE population >= ANY (SELECT population FROM bbc y WHERE x.region = y.region)
    or:  SELECT name FROM bbc x WHERE population = (SELECT max(population) FROM bbc y WHERE x.region = y.region)
    or:  SELECT name FROM bbc WHERE population IN (SELECT max(population) FROM bbc GROUP BY region)
    3b. 給出地區中所有國家的人口總數為0的地區
        SELECT region FROM bbc WHERE 0 = ALL(SELECT sum(population) FROM bbc GROUP BY region)
    or:  SELECT region FROM bbc x WHERE 0 = (SELECT sum(population) FROM bbc y WHERE x.region = y.region)
    or:  SELECT region FROM bbc GROUP BY region HAVING sum(population) = 0
    3c. 有些國家的人口數比她的周邊國家(周邊國家指在同一地區的國家)要多三倍,列出這些國家和地區
        SELECT name,region FROM bbc x WHERE population > all(SELECT population*3 FROM bbc y WHERE x.region=y.region AND x.name!=y.name)

三:More about SELECThttp://www.sqlzoo.cn/1b.htm
    1a. 修改下面的查詢,顯示1950諾貝爾獎得主
        SELECT yr, subject, winner  FROM nobel WHERE yr = 1950
    1b. 給出1962 文學獎獲得者
        SELECT winner FROM nobel WHERE yr = 1962 AND subject = 'Literature'
    2a. 顯示'Albert Einstein'獲獎的年份和獎項
        SELECT yr,subject FROM nobel WHERE winner = 'Albert Einstein'
    2b. 給出2000年以來(包含2000年)和平獎的得主
        SELECT winner FROM nobel WHERE yr >= 2000 AND subject ='Peace'
    2c. 給出1980 到 1989間文學獎獲得者的所有資訊
        SELECT * FROM nobel WHERE subject='Literature' AND yr BETWEEN 1980 AND 1989
    2d. 給出獲獎者為下列總統的所有資訊. ('Theodore Roosevelt', 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter')
        SELECT * FROM nobel WHERE winner IN ('Theodore Roosevelt', 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter')
    2e. 名字的第一個字元為John的獲獎者名稱
        SELECT winner FROM nobel WHERE winner LIKE 'John%'

四:Aggregate functionshttp://www.sqlzoo.cn/2.htm
    1a. 顯示世界人口總和
        SELECT SUM(population) FROM bbc
    1b. 列出所有的地區,但每個地區只能顯示一次
        SELECT DISTINCT region FROM bbc
    or:  SELECT region FROM bbc GROUP BY region
    1c. 顯示GDP超過所有Africa(非洲)國家總和的國家
        SELECT name FROM bbc WHERE gdp > (SELECT SUM(gdp) FROM bbc WHERE region = 'Africa')
    2a. 顯示每個地區以及的該地區的國家總數
        SELECT region, COUNT(name) FROM bbc GROUP BY region
    2b. 顯示每個地區以及的該地區國家總人口數不少於1000萬的國家總數
        SELECT region, COUNT(name) FROM bbc WHERE population >=10000000 GROUP BY region
    or:  SELECT region, COUNT(name) FROM (SELECT region, name FROM bbc WHERE population>=10000000)temp GROUP BY region
    2c. 列出人口總數不少於1億的地區
        SELECT region FROM bbc GROUP BY region HAVING SUM(population)>=100000000

五:How to do joinshttp://sqlzoo.cn/3b.htm
    1a. Show the athelete (who) and the country name for medal winners in 2000
        SELECT who, country.name FROM ttms 
      JOIN country ON (ttms.country=country.id) WHERE games = 2000
    1b. Show the who and the color of the medal for the medal winners from 'Sweden'
        SELECT who, color FROM ttms 
      JOIN country ON (ttms.country=country.id) WHERE country.name = 'Sweden'
    1c. Show the years in which 'China' won a 'gold' medal
        SELECT games FROM ttms JOIN country ON (ttms.country = country.id)
      WHERE name = 'China' AND color = 'gold'
    2a. Show who won medals in the 'Barcelona' games
        SELECT who FROM ttws JOIN games ON (ttws.games=games.yr) WHERE city = 'Barcelona'
    2b. Show which city 'Jing Chen' won medals. Show the city and the medal color
        SELECT city,color FROM games JOIN ttws ON( ttws.games=games.yr) WHERE who = 'Jing Chen'
    2c. Show who won the gold medal and the city
        SELECT who, city FROM ttws JOIN games ON(ttws.games = games.yr) WHERE color = 'gold'
    3a. Show the games and color of the medal won by the team that includes 'Sen Yan'
        SELECT games,color FROM ttmd JOIN team ON(ttmd.team = team.id) WHERE team.name = 'Sen Yan'
    3b. Show the 'gold' medal winners in 2004
        SELECT name FROM team JOIN ttmd ON(ttmd.team = team.id) WHERE color = 'gold' AND games = 2004
    3c. Show the name of each medal winner country 'FRA'
        SELECT name FROM team JOIN ttmd ON(ttmd.team = team.id) WHERE country = 'FRA'

六:The JOIN operationhttp://sqlzoo.cn/3.htm
    1a. 列出獲得不少於30000 votes(選票)的電影. [顯示 title, votes] 
        SELECT title, votes FROM movie WHERE votes>=30000
    1b. 電影'Citizen Kane'的首映年份
        SELECT yr FROM movie WHERE title = 'Citizen Kane'
    1c. 列出包含the Police Academy(警校)字樣的title(電影名稱)和 score(得分) films. [The films have a title that is LIKE 'Police Academy%'] 
        SELECT title,score FROM movie Where title LIKE 'Police Academy%'
    1d. 列出所有the Star Trek movies(星際系列電影),顯示title(電影標題)和score(得分). 按電影的發行 yr(年份)排序
        SELECT title,score FROM movie WHERE title LIKE '%Star Trek%' ORDER BY yr ASC
    1e. 列出名稱中包含'Dog'的電影名和得分
        SELECT title,score FROM movie WHERE title LIKE '%Dog%'
    2a. 列出id為 1, 2, 3的電影的名稱
        SELECT title FROM movie WHERE id in (1,2,3)
    2b. 電影'Glenn Close' 的ID號是多少? 
        SELECT id FROM actor WHERE name= 'Glenn Close'
    2c. 電影'Casablanca' 的ID號是多少? 
        SELECT id FROM movie WHERE title = 'Casablanca'
    3a. 顯示id=1的電影'Star Wars'(星球大戰)中演員(角色)名單
        SELECT name FROM casting, actor WHERE movieid=1 AND actorid=actor.id
    or:  SELECT name FROM movie, casting, actor WHERE movie.title = 'Star Wars' AND movie.id = casting.movieid AND casting.actorid = actor.id
    3b. 顯示電影'Alien'的演員(角色)名單
        SELECT name FROM actor, casting, movie
WHERE movie.title = 'Alien' AND movie.id = casting.movieid AND casting.actorid = actor.id
    3c. 顯示有'Harrison Ford'參演的電影名稱
        SELECT title FROM movie 
      JOIN actor ON actor.name = 'Harrison Ford' 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id 
    3d. 顯示有'Harrison Ford'參演的電影名稱,但'Harrison Ford'在該影片中不是主角(即在角色表中不是排名第一)
        SELECT title FROM movie 
      JOIN actor ON actor.name = 'Harrison Ford' 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id AND casting.ord != 1
    3e. 顯示1962年發行的電影以及該影片中主角(即在角色表中排名第一)
        SELECT title,name FROM actor 
      JOIN movie ON movie.yr = 1962 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id AND casting.ord = 1
    4a. 'John Travolta'哪一年最忙? 顯示出他每年的出演電影數量
        SELECT yr,COUNT(title) FROM movie 
      JOIN actor ON actor.name = 'John Travolta' 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id 
      GROUP BY yr
    4b. 列出有'Julie Andrews'參演的所有電影名稱以及該片的主角
        SELECT title, name FROM movie, casting, actor WHERE casting.movieid = movie.id AND casting.actorid = actor.id AND casting.ord = 1 AND casting.movieid IN (SELECT movieid FROM casting, actor WHERE casting.actorid = actor.id AND actor.name = 'Julie Andrews')
    4c. 列出那些最起碼在10部電影中出任過主角的演員
        SELECT name FROM casting 
      JOIN actor ON  casting.actorid = actor.id WHERE casting.ord=1
      GROUP BY name HAVING COUNT(movieid)>=10
    or:  SELECT name FROM actor,casting,movie 
        WHERE casting.actorid = actor.id AND casting.ord = 1 AND movie.id = casting.movieid 
        GROUP BY name HAVING COUNT(casting.movieid)>=10
    4d. 按演員人數的多少依次列出1978 的電影以及演員數.人數最多排在最前
        SELECT title,count(actorid) FROM movie, casting WHERE casting.movieid = movie.id AND movie.yr = 1978
      GROUP BY title ORDER BY 2 DESC
    4e. 列出所有和'Art Garfunkel'合作過的演員(不要列出'Art Garfunkel'他自己)
        SELECT DISTINCT name FROM actor,casting WHERE actor.id = casting.actorid 
      AND casting.movieid IN (SELECT movieid FROM casting,actor 
        WHERE casting.actorid = actor.id AND actor.name = 'Art Garfunkel')
      AND name != 'Art Garfunkel'