SQLZOO翻译及其代码答案(2021/1/17更)

耗尽温柔 提交于 2021-01-19 12:57:00

SQL入门小白的练习作业整理(●’◡’●)

0 SELECT basics

表:
world(name,continent.area,population,gdp,capital)

#1(考察where) 列出德国的人口

SELECT population FROM world 
  WHERE name = 'Germany';

#2(考察in) 列出’Sweden’, ‘Norway’ 和 'Denmark’地区的名字和人口

SELECT name, population FROM world
  WHERE name IN ( 'Sweden', 'Norway', 'Denmark');

#3(考察between) 列出面积为200000-250000之间的国家名和面积

SELECT name, area FROM world
  WHERE area BETWEEN 200000 AND 250000;

1 SELECT names

表:
world(name,continent.area,population,gdp,capital)

#1 列出Y开头的国家名

SELECT name FROM world
  WHERE name LIKE 'Y%';

#2 列出Y结尾的国家名

SELECT name FROM world
  WHERE name LIKE '%Y';

#3 列出含有字母x的国家名

SELECT name FROM world
  WHERE name LIKE '%x%';

#4 列出land结尾的国家名

SELECT name FROM world
  WHERE name LIKE '%land';

#5 列出开头是C,结尾是ia的国家名

SELECT name FROM world
  WHERE name LIKE 'C%ia';

#6 列出名字里含有oo的国家名

SELECT name FROM world
  WHERE name LIKE '%oo%';

#7 列出名字里含有3个以上a的国家名

SELECT name FROM world
  WHERE name LIKE '%a%a%a%';

#8 列出名字里第二个字母是t的国家名,按照国家名排序

SELECT name FROM world
  WHERE name LIKE '_t%'
  ORDER BY name;

#9 列出名字里有两个字母o中间相隔2个字符的国家名

SELECT name FROM world
  WHERE name LIKE '%o__o%'

#10 列出名字为四个字母的国家名

SELECT name FROM world
  WHERE name LIKE '____';

#11 列出国家名和首都名称一致的国家名

SELECT name FROM world
  WHERE name = capital;

#12 列出首都名是国家名加上’City’的国家名(City前要加空格)

SELECT name FROM world
  WHERE concat(name,' City') = capital;

#13 列出首都名和国家名,其中首都名包含有国家名

SELECT capital, name FROM world
  WHERE capital LIKE concat('%',name,'%');

#14 列出首都名和国家名,其中首都名是国家名的延申

SELECT capital, name FROM world
  WHERE capital LIKE concat(name,'_','%');

#15(新增REPLACE函数)在14T的筛选基础上,列出国家名,国家名的延申部分(extension)

SELECT name,REPLACE(capital,name,'') AS extension
  FROM world
  WHERE capital LIKE concat(name,'_','%');

2 SELECT from World

表:
world(name,continent.area,population,gdp,capital)

#1 略

#2 列出人口数至少200000000的国家

SELECT name FROM world
  WHERE population >= 200000000;

#3 列出人口数至少200000000的国家名以及人均GDP

SELECT name,gdp/population AS 'per capita GDP'
  FROM world
  WHERE population >= 200000000;

#4 列出南美大陆的国家名和人口数(以百万为单位)

SELECT name,population/1000000 AS 'population in millions'
  FROM world
  WHERE continent = 'South America';

#5 列出rance, Germany, Italy及其人口数

SELECT name, population From world
  WHERE name IN ('France','Germany','Italy');

#6 列出名字含有’United’的国家名

SELECT name FROM world
  WHERE name LIKE '%United%';

#7 列出面积超过3百万或者人口超过250百万的国家名,人口数和面积

SELECT name, population, area FROM world
  WHERE area > 3000000
    OR population > 250000000;

#8 列出面积超过3百万或者人口超过250百万(不能两者同时满足)的国家名,人口数和面积

SELECT name, population, area FROM world
  WHERE (area > 3000000
    AND population <= 250000000)
    OR(area <= 3000000
    AND population > 250000000);

#9(新增ROUND函数)列出南美地区的国家名,人口数(单位为百万),GDP(单位为十亿),并保留两位小数

SELECT name, ROUND(population/1000000,2), ROUND(gdp/1000000000,2)
  FROM world
  WHERE continent = 'South America';

#10 列出GDP至少1000000000000的国家名和人均GDP(保留到千位数)

SELECT name, ROUND(gdp/population,-3) AS 'per-capta GDP'
  FROM world
  WHERE gdp >= 1000000000000;

#11(新增LENGTH函数) 列出国家名和首都名长度一致的国家名和首都名

SELECT name, capital FROM world
  WHERE LENGTH(name) = LENGTH(capital);

#12(新增LEFT函数)列出国家名和首都名首字母一致且两者不完全相等的国家名和首都名

SELECT name, capital FROM world
  WHERE LEFT(name,1) = LEFT(capital,1)
    AND name <> capital;

#13 列出含有所有元音字母‘aeiou’且不含空格的国家名

SELECT name FROM world
  WHERE name LIKE '%a%'
    AND name LIKE '%e%'
    AND name LIKE '%i%'
    AND name LIKE '%o%'
    AND name LIKE '%u%'
    AND name NOT LIKE '% %';

3 SELECT from Nobel

表:
nobel(yr,subject,winner)

#1 列出1950年的诺贝尔奖奖项和得主

SELECT yr, subject, winner FROM nobel
  WHERE yr = 1950;

#2 列出1962年获得诺贝尔文学奖的人

SELECT winner FROM nobel
  WHERE yr = 1962
  AND subject = 'Literature';

#3 列出 ‘Albert Einstein’ 的获奖年份与奖项

SELECT yr, subject FROM nobel
  WHERE winner =  'Albert Einstein';

#4 列出2000年及其以后的诺贝尔和平奖得主

SELECT winner FROM nobel
  WHERE yr >= 2000
  AND subject = 'Peace';

#5 列出1980-1989年获得诺贝尔文学奖的所有信息

SELECT * FROM nobel
  WHERE subject = 'Literature'
  AND yr BETWEEN 1980 AND 1989;

#6 列出获奖者为以下几人的所有信息

SELECT * FROM nobel
 WHERE winner IN ('Theodore Roosevelt',
                  'Woodrow Wilson',
                  'Jimmy Carter', 
                  'Barack Obama'
                  );

#7 列出First name是John的获奖者

SELECT winner FROM nobel
  WHERE winner LIKE 'John %';

#8 列出1980年获得物理学奖或者1984年获得化学奖的所有信息

SELECT * FROM nobel
  WHERE (yr = 1980 AND subject = 'Physics')
  OR (yr = 1984 AND subject = 'Chemistry');

#9 列出1980年得奖的所有信息(不包括化学奖和医药学奖)

SELECT * FROM nobel
  WHERE yr = 1980
  AND subject NOT IN ('Chemistry','Medicine');

#10 列出所有信息(条件:奖项为医药学并且是1910年以前(不包括1910),或者奖项为文学并且是2004年以后(包括2004年))

SELECT * FROM nobel
  WHERE (subject = 'Medicine' AND yr < 1910)
  OR (subject = 'Literature' AND yr >=2004);

#11 列出得奖者为PETER GRÜNBERG的所有信息

SELECT * FROM nobel
  WHERE winner = 'PETER GRÜNBERG';

#12 列出得奖者为EUGENE O’NEILL的所有信息

SELECT * FROM nobel
  WHERE winner = 'EUGENE O\'NEILL';

(\为转义符,放在要转义的字符前)

#13 列出得奖者前缀为Sir的winner,yr,subject信息,并且按照时间最近顺序排序,然后按照得奖者名字排序

SELECT winner, yr, subject FROM nobel
  WHERE  winner LIKE 'Sir%'
  ORDER BY yr DESC, winner;

#14 列出1984年诺贝尔得奖的winner和subject,按照subject和winner排序,但是把化学奖和物理学奖信息列在最后

SELECT winner, subject FROM nobel
  WHERE yr = 1984
  ORDER BY subject IN ('Chemistry','Physics'), subject, winner;

4 SELECT within SELECT

表:
world(name,continent.area,population,gdp,capital)

#1 列出人口数大于俄罗斯人口数的国家名

SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')

#2 列出欧洲人均GDP大于英国的国家名

SELECT name FROM world
  WHERE gdp/population >
     (SELECT gdp/population FROM world
      WHERE name='United Kingdom')
  AND continent = 'Europe';

#3 列出大陆与’Argentina’, 'Australia’一致的所有国家名和大陆名

SELECT name, continent FROM world
  WHERE continent IN(SELECT continent FROM world WHERE name IN('Argentina', 'Australia'))
  ORDER BY name;

#4 列出人口数超过加拿大但小于波兰的国家和人口数

SELECT name, population FROM world
  WHERE population > (SELECT population FROM world 
  					    WHERE name = 'Canada') 
  AND population < (SELECT population FROM world 
  	                  WHERE name = 'Poland');

#5 列出欧洲国家的国家名,以及人口相对于德国的人口数的百分比

SELECT name, CONCAT(ROUND(100*population/(SELECT population 
										  FROM world 
										  WHERE name = 'Germany'),0),'%') AS percentage 
  FROM world
  WHERE continent = 'Europe';

#6 列出gdp大于欧洲所有国家的国家名

SELECT name FROM world
  WHERE gdp > ALL(SELECT IFNULL(gdp,0) 
  	              FROM world 
  	              WHERE continent = 'Europe'
  	              );

#7 列出各个大陆面积最大的国家的continent,name,area

SELECT continent, name, area FROM world x
  WHERE area >= ALL(SELECT area 
  					FROM world y 
  					WHERE y.continent = x.continent
  					);

#8* 列出每个大陆按照国家名排序的第一个国家的大陆名和国家名

SELECT continent, name FROM world x
  WHERE name = (SELECT name 
  				FROM world y 
  				WHERE y.continent = x.continent 
  				ORDER BY name 
  				LIMIT 1
  				);

#9* 找出哪些大陆里每一个国家的人口数都小于25000000,列出这些大陆的name,continent,population信息

SELECT name, continent, population FROM world x
  WHERE 25000000 > ALL(SELECT population 
  	   				   FROM world y 
  	   				   WHERE y.continent = x.continent
  	   				   );

#10* 列出国家人口数超过该大陆内任何一个国家(不包括自己)人口数三倍的国家名和大陆名

SELECT name, continent FROM world x
  WHERE population > ALL(SELECT population*3 
  						 FROM world y 
  						 WHERE y.continent = x.continent
  						 AND y.name <> x.name
  						 );

5 SUM and COUNT

表:
world(name,continent.area,population,gdp,capital)

#1 列出全世界总人口

SELECT SUM(population) FROM world;

#2 列出所有大陆(不重复)

SELECT DISTINCT continent FROM world;

#3 列出非洲大陆的总GDP

SELECT SUM(gdp) FROM world
  WHERE continent = 'Africa';

#4 有多少国家的面积大于1000000

SELECT COUNT(*) FROM world
  WHERE area >= 1000000;

#5 ‘Estonia’, ‘Latvia’, 'Lithuania’一共有多少人口

SELECT SUM(population) FROM world
  WHERE name IN ('Estonia', 'Latvia', 'Lithuania');

#6 列出每个大陆及其国家数量

SELECT continent, COUNT(*) FROM world
  GROUP BY continent;

#7 列出每个大陆及其人口大于10000000的国家数量

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

#8 列出总人口至少100百万的大陆

SELECT continent FROM world
  GROUP BY continent
  HAVING SUM(population) >= 100000000;

6 JOIN

表:
game(id,mdate,stadium,team1,team2)
goal(matchid,teamid,player,gtime)
eteam(id,teamname,coach)


#1 列出Germany队的比赛场次,球员信息

SELECT matchid, player FROM goal 
  WHERE teamid = 'GER';

#2 列出1012比赛的id, stadium, team1, team2信息

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

#3 列出每次德国进球的player, teamid, stadium mdate

SELECT player, teamid, stadium, mdate
  FROM game AS ga INNER JOIN goal AS go
  ON ga.id = go.matchid
  WHERE teamid = 'GER';

#4 列出球员名字前缀是Mario的每场进球的team1, team2 player信息

SELECT team1, team2, player
  FROM game AS ga INNER JOIN goal AS go
  ON ga.id = go.matchid
  WHERE player LIKE 'Mario%';

#5 列出所有10分钟以内进球的player, teamid, coach, gtime信息

SELECT player, teamid, coach, gtime
  FROM goal AS go INNER JOIN eteam AS e
  ON go.teamid = e.id
  WHERE gtime <= 10;

#6 列出team1教练是’Fernando Santos’的比赛的时间以及队伍名称

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

#7 列出在’National Stadium, Warsaw’参加比赛的球员名字

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

#8 列出所有与德国对决的比赛中进球的球员名字(不包括德国队球员)

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

#9 列出所有队伍名字及其进球次数

SELECT teamname, COUNT(*)
  FROM goal AS go INNER JOIN eteam AS e
  ON go.teamid = e.id
  GROUP BY teamname;

#10 列出每一个体育场及在该体育场的进球次数

SELECT stadium, COUNT(*)
  FROM game AS ga INNER JOIN  goal AS go
  ON ga.id = go.matchid
  GROUP BY stadium;

#11 列出每一场’POL’参与的比赛编码,时间以及进球次数

SELECT matchid, mdate, COUNT(*)
  FROM game AS ga INNER JOIN goal AS go
  ON ga.id = go.matchid
  WHERE team1 = 'POL' OR team2 = 'POL'
  GROUP BY matchid, mdate;

#12 每一场’GER’进球的比赛里,列出matchid,mdate,以及’GER’队的进球次数

SELECT matchid, mdate, COUNT(*)
  FROM game AS ga INNER JOIN goal AS go
  ON ga.id = go.matchid
  AND teamid = 'GER'
  GROUP BY matchid, mdate;

#13(新增CASE WHEN) 列出每场比赛两支队伍各自的进球次数

SELECT mdate,team1,
  SUM(CASE WHEN team1 = teamid 
  	  THEN 1
  	  ELSE 0
  	  END) AS score1,team2,
  SUM(CASE WHEN team2 = teamid 
  	  THEN 1
  	  ELSE 0
  	  END) AS score2
  FROM game AS ga LEFT OUTER JOIN goal AS go
  ON ga.id = go.matchid
  GROUP BY mdate, matchid, team1, team2;

7 More Join operations

表:
movie(id,title,yr,director,budget,gross)
actor(id,name)
casting(movieid,actorid,ord)


#1 略

#2 列出’Citizen Kane’的年份

SELECT yr FROM movie WHERE title = 'Citizen Kane';

#3 列出所有名字含有’Star Trek’的电影的id,title,yr信息,并以年排序

SELECT id, title, yr FROM movie
  WHERE title LIKE '%Star Trek%'
  ORDER BY yr;

#4 列出’Glenn Close’演员的id

SELECT id FROM actor
  WHERE name = 'Glenn Close';

#5 列出电影’Casablanca’的id

SELECT id FROM movie
  WHERE title = 'Casablanca';

#6 列出电影’Casablanca’的演员名单

SELECT name 
  FROM actor AS a INNER JOIN casting AS c
    ON a.id = c.actorid
    WHERE movieid = (SELECT id FROM movie
  					   WHERE title = 'Casablanca');

#7 列出电影’Alien’的演员名单

SELECT name 
  FROM actor AS a INNER JOIN casting AS c
  ON a.id = c.actorid
  WHERE movieid = (SELECT id FROM movie
  					 WHERE title = 'Alien');

#8 列出演员 'Harrison Ford’出演过的电影

SELECT title
  FROM movie AS m INNER JOIN casting AS c
  ON m.id = c.movieid
  WHERE actorid = (SELECT id FROM actor 
  				     WHERE name = 'Harrison Ford');

#9 列出演员 'Harrison Ford’出演过的电影(条件是ord<>1)

SELECT title
  FROM movie AS m INNER JOIN casting AS c
  ON m.id = c.movieid
  WHERE actorid = (SELECT id FROM actor 
  				     WHERE name = 'Harrison Ford')
  AND ord <> 1;

#10 列出1962年的电影以及主角名(ord=1)

SELECT title, name
  FROM movie AS m INNER JOIN casting AS c
  	ON m.id = c.movieid
  INNER JOIN actor AS a
  	ON c.actorid = a.id
  WHERE m.yr = 1962
  AND ord = 1;

#11 列出’Rock Hudson’每年的作品数量(筛选出超过两部作品的年份)

SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
        JOIN actor   ON actorid=actor.id
WHERE name='Rock Hudson'
GROUP BY yr
HAVING COUNT(title) > 2;

#12 列出’Julie Andrews’演过的所有电影以及主角(ord=1)

SELECT title,name FROM 
  movie AS m INNER JOIN casting AS c ON m.id = c.movieid
			 INNER JOIN actor AS a ON c.actorid = a.id
WHERE m.id IN (SELECT movieid FROM casting
			   WHERE actorid IN (SELECT id FROM actor
								 WHERE name='Julie Andrews'))
AND ord = 1;

#13 列出至少有15部电影为主角的演员名单,按照字母排序

SELECT name FROM 
  movie AS m INNER JOIN casting AS c ON m.id = c.movieid
			 INNER JOIN actor AS a ON c.actorid = a.id
WHERE ord = 1
GROUP BY name
HAVING COUNT(*) >= 15
ORDER BY name;

#14 列出1978年的电影作品及其演员数量,要求用演员数量从多到少排序,再按照电影名字排序

SELECT title,COUNT(*)
FROM movie AS m INNER JOIN casting AS c
  ON m.id = c.movieid
INNER JOIN actor AS a
  ON c.actorid = a.id
WHERE yr = 1978
GROUP BY title
ORDER BY COUNT(*) DESC,title

#15 列出与’Art Garfunkel’合作过的所有演员

SELECT DISTINCT name
  FROM actor AS a INNER JOIN casting AS c
  	ON a.id = c.actorid
WHERE movieid IN (SELECT m.id
  				  FROM movie AS m INNER JOIN casting AS c
  					ON m.id = c.movieid
  				  WHERE actorid = (SELECT id FROM actor 
  				     			   WHERE name =  'Art Garfunkel'))
AND name <> 'Art Garfunkel';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!