sql在线练习网站(http://sqlzoo.cn)答案解析(1)
一: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%'
sql在线练习网站(http://sqlzoo.cn)答案解析(2)
sql在线练习网站(http://sqlzoo.cn)答案解析(3)
sql在线练习网站(http://sqlzoo.cn)答案解析(4)