sql在线练习网站(http://sqlzoo.cn)答案解析

一:SELECT(http://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 SELECT(http://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 SELECT(http://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 joins(http://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 operation(http://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'



你可能感兴趣的:(mysql)