本文是小编在自学SQL过程中,做完SQLZOO Tutorial练习后整理的答案,仅供参考。

  1. 目录

    select basics

    select from world

    select from nobel

    select in select

    sum and count

    join

    more join

    using null

    self join


    select basics

1. The example uses a WHERE clause to show the population of 'France'. Note that strings                (pieces of text that are data) should be in 'single quotes';

Modify it to show the population of Germany

SELECT population FROM worldWHERE name = 'Germany'

2.

Checking a list The word IN allows us to check if an item is in a list. The example shows the         name and population for the countries 'Brazil', 'Russia', 'India' and 'China'.

Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.

SELECT name, population FROM worldWHERE name IN ('Sweden', 'Norway', 'Denmark')

3. Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to show the country and the area for countries with an area between 200,000 and 250,000.

SELECT name, area FROM worldWHERE area BETWEEN 200000 and 250000

select from world

1. Observe the result of running this SQL command to show the name, continent and population of all countries.

SELECT name, continent, population FROM world

2. Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.

select name from world
where population > 200000000

3.Give the name and the per capita GDP for those countries with a population of at least 200 million.

HELP:How to calculate per capita GDP

select name, gdp/population
from world
where population >= 200000000

select from nobel

1. Change the query shown so that it displays Nobel prizes for 1950.

select yr, subject, winner from nobel
where yr = 1950

2. Show who won the 1962 prize for Literature.

select winner from nobel
where yr = 1962 and subject = 'Literature'

3. Show the year and subject that won 'Albert Einstein' his prize

select yr, subject from nobel
where winner = 'Albert Einstein'

4. Give the name of the 'Peace' winners since the year 2000, including 2000.

select winner from nobel
where subject = 'Peace' and yr >= 2000

select in select

1. List each country name where the population is larger than that of 'Russia'.

world(name, continent, area, population, gdp)
SELECT name FROM bbcWHERE population > ALL(SELECT MAX(population)FROM bbcWHERE region = 'Europe')AND region = 'South Asia'

2. Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.

Per Capita GDP

select name from world
where continent = 'Europe'
and gdp/population >
(select gdp/population from world
where name = 'United Kingdom')

3. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.

select name, continent from world
where continent in
(select distinct(continent) from world
where name = 'Argentina'
or name = 'Australia')
order by name

4. Which country has a population that is more than Canada but less than Poland? Show the name and the population.

select name, population
from world
where
population > (select population from world where name = 'Canada')
and
population < (select population from world where name = 'Poland')

5.Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

The format should be Name, Percentage for example:

Decimal places

You can use the function ROUND to remove the decimal places.

Percent symbol %

select name, concat(round(population * 100 /(select population from world where name = 'Germany'),0),'%') as percentage from world
where continent = 'Europe'

6. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)

select name from world
where gdp > all (select gdp from world where gdp != 'null' and continent = 'Europe')

7. Find the largest country (by area) in each continent, show the continent, the name and the area:

select continent, name, area from world w1
where w1.area >= all(select area from world w2 where w2.continent = w1.continent ) 

8. List each continent and the name of the country that comes first alphabetically.

select continent,name from world w1
where w1.name = (select w2.name from world w2 where w1.continent = w2.continent order by name limit 1)

9. Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show namecontinent and population.

select name,continent,population from world
where continent not in
(select distinct(continent) from world where population > 25000000)

10. Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.

select w1.name, w1.continent
from world w1
where w1.population / 3
>= all(select w2.population from world w2
where w1.continent = w2.continent
and w1.name != w2.name)

sum and count

1. Show the total population of the world.

world(name, continent, area, population, gdp)
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  = '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

join

1. The first example shows the goal scored by a player with the last name 'Bender'. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime

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

2. From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.

Notice in the that the column matchid in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table.

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

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

3. You can combine the two steps into a single query with a JOIN.

The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.

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

SELECT player,teamid,stadium,mdateFROM game JOIN goal ON (game.id = goal.matchid)
where teamid = 'GER'

4. Use the same JOIN as in the previous question.

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(game.id = goal.matchid)
where player like 'Mario%'

5. The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id

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

select player,teamid,coach,gtime
from eteam join goal on (goal.teamid = eteam.id)
where gtime <= 10

6. To JOIN game with eteam you could use either
game JOIN eteam ON (team1=eteam.id) or game JOIN eteam ON (team2=eteam.id)

Notice that because id is a column name in both game and eteam you must specify eteam.id instead of just id

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

select mdate,teamname
from eteam join game on (game.team1 = eteam.id)
where coach = 'Fernando Santos'

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

select player
from game join goal on (game.id = goal.matchid)
where stadium = 'National Stadium, Warsaw'

8.

The example query shows all goals scored in the Germany-Greece quarterfinal.

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

HINT

Select goals scored only by non-German players in matches where GER was the id of either team1 or team2.

You can use teamid!='GER' to prevent listing German players.

You can use DISTINCT to stop players being listed twice.

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.

COUNT and GROUP BY

select teamname,count(player) as goal
from goal join eteam on (teamid = id)
group by teamname

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

select stadium, count(id) as goals
from game join goal on (game.id = goal.matchid)
group by stadium

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

select matchid,mdate,count(gtime) as goals
from game join goal on (id = matchid)
where 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(gtime) as goals
from game join goal on (id = matchid)
where teamid = 'GER'
group by matchid,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.

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 (matchid = id)
group by id,mdate,team1,team2
order by mdate,matchid,team1,team2

more join

1. List the films where the yr is 1962 [Show idtitle]

select id,title from movie
where yr = 1962

2. Give year of 'Citizen Kane'.

select yr from movie
where title = 'Citizen Kane'

3. List all of the Star Trek movies, include the idtitle and yr (all of these movies include the words Star Trek in the title). Order results by year.

select id,title,yr
from movie
where title like 'Star Trek%'
order by yr

4. What id number does the actor 'Glenn Close' have?

select id
from actor
where name = 'Glenn Close'

5. What is the id of the film 'Casablanca'

select id from movie
where title = 'Casablanca'

6. Obtain the cast list for 'Casablanca'.

what is a cast list?

Use movieid=11768, (or whatever value you got from the previous question)

select name
from actor join casting on (actor.id = casting.actorid)
where movieid = 11768

7. Obtain the cast list for the film 'Alien'

select name
from movie m,actor a, casting c
where m.id = c.movieid
and a.id = c.actorid
and m.title = 'Alien'

8. List the films in which 'Harrison Ford' has appeared

select title
from movie m, actor a,casting c
where m.id = c.movieid
and a.id = c.actorid
and a.name = 'Harrison Ford'

9. List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]

select title
from movie m, actor a,casting c
where m.id = c.movieid
and a.id = c.actorid
and a.name = 'Harrison Ford'
and c.ord != 1

10. List the films together with the leading star for all 1962 films.

select title, name
from movie m, actor a,casting c
where m.id = c.movieid
and a.id = c.actorid
and m.yr = 1962
and c.ord = 1

11. Which were the busiest years for 'Rock Hudson', show the year and the number of movies he made each year for any year in which he made more than 2 movies.

select yr, count(m.title)
from movie m
join casting c on (m.id = c.movieid)
join actor a on (a.id = c.actorid)
where a.name = 'Rock Hudson'
group by yr
having count(m.title) > 2

12. List the film title and the leading actor for all of the films 'Julie Andrews' played in.

Did you get "Little Miss Marker twice"?

select m.title, a.name
from movie m
join casting c on (c.movieid = m.id)
join actor a on (a.id = c.actorid)
where c.ord = 1
and  m.id in
(select m.id
from movie m
join casting c on (c.movieid = m.id)
join actor a on (a.id = c.actorid)
where a.name = 'Julie Andrews')

13. Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.

select a.name
from casting c
join movie m on (m.id = c.movieid)
join actor a on (c.actorid = a.id)
where c.ord = 1
group by a.name
having count(m.id) >= 15
order by a.name

14. List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

select m.title, count(a.id)
from movie m
join casting c on (m.id = c.movieid)
join actor a on (a.id = c.actorid)
where m.yr = 1978
group by m.title
having count(m.id)
order by count(a.id) desc,title

15. List all the people who have worked with 'Art Garfunkel'.

select a.name
from movie m
join casting c on (c.movieid = m.id)
join actor a on (a.id = c.actorid)
where a.name != 'Art Garfunkel'
and m.id in
(select m.id
from movie m
join casting c on (c.movieid = m.id)
join actor a on (a.id = c.actorid)
where a.name = 'Art Garfunkel')

using null

1. List the teachers who have NULL for their department.

Why we cannot use =

select name
from teacher
where dept is null

2. Note the INNER JOIN misses the teachers with no department and the departments with no teacher.

select t.name, d.name
from teacher t
inner join dept d on (t.dept = d.id)

3. Use a different JOIN so that all teachers are listed.

select teacher.name,dept.name
from teacher
left join dept
on (teacher.dept = dept.id)

4. Use a different JOIN so that all departments are listed.

select teacher.name, dept.name
from teacher
right join dept
on (teacher.dept = dept.id)

5. Use COALESCE to print the mobile number. Use the number '07986 444 2266' if there is no number given. Show teacher name and mobile number or '07986 444 2266'

select name, coalesce(mobile,'07986 444 2266') as mobile
from teacher

6. Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department.

select t.name, coalesce(d.name,'None')
from teacher t left join dept d
on (t.dept = d.id)

7. Use COUNT to show the number of teachers and the number of mobile phones.

select count(name), count(mobile)
from teacher

8. Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed.

select dept.name, count(teacher.name)
from teacher right join dept
on (teacher.dept = dept.id)
group by dept.name

9. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise.

select teacher.name,
case when (teacher.dept in (1,2))
then 'Sci'
else 'Art'
end
from teacher left join dept on (teacher.dept = dept.id)

10. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise.

select teacher.name,
case when (teacher.dept in (1,2))
then 'Sci'
when (teacher.dept = 3)
then 'Art'
else 'None'
end
from teacher left join dept
on (teacher.dept = dept.id)

self join

1. How many stops are in the database.

select count(name)
from stops

2. Find the id value for the stop 'Craiglockhart'

select id
from stops
where name = 'Craiglockhart'

3. Give the id and the name for the stops on the '4' 'LRT' service.

select id, name
from stops join route
on (id = stop)
where num = '4'
and company = 'LRT'

4. The query shown gives the number of routes that visit either London Road (149) or Craiglockhart (53). Run the query and notice the two services that link these stops have a count of 2. Add a HAVING clause to restrict the output to these two routes.

select company, num, count(*)
from route where stop=149 or stop=53
group by company, numhaving count(*) = 2

5. Execute the self join shown and observe that b.stop gives all the places you can get to from Craiglockhart, without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.

select a.company, a.num, a.stop, b.stop
from route a join route b on (a.company=b.company and a.num=b.num)
where a.stop=53 and b.stop = 149

6. The query shown is similar to the previous one, however by joining two copies of the stops table we can refer to stops by name rather than by number. Change the query so that the services between 'Craiglockhart' and 'London Road' are shown. If you are tired of these places try 'Fairmilehead' against 'Tollcross'

select a.company, a.num, stopa.name, stopb.name
from route a join route b on(a.company=b.company and a.num=b.num)join stops stopa on (a.stop=stopa.id)join stops stopb on (b.stop=stopb.id)
where stopa.name = 'Fairmilehead' and stopb.name = 'Tollcross'

7. Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')

select distinct(a.company), a.num
from route a inner join route b
on (a.company = b.company)
and (a.num = b.num)
and a.stop = 115
and b.stop = 137

8. Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'

select R1.company,R1.numfrom route R1,route R2,stops S1,stops S2where R1.company = R2.companyand R1.num = R2.numand R1.stop = S1.idand R2.stop = S2.idand S1.name = 'Craiglockhart'and S2.name = 'Tollcross'

9. Give a distinct list of the stops which may be reached from 'Craiglockhart' by taking one bus, including 'Craiglockhart' itself, offered by the LRT company. Include the company and bus no. of the relevant services.

select distinct S2.name, R2.company, R2.num
from stops S1, stops S2, route R1, route R2
where S1.name = 'Craiglockhart'and S1.id=R1.stopand R1.company=R2.company and R1.num=R2.numand R2.stop=S2.id

10. Find the routes involving two buses that can go from Craiglockhart to Lochend.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus.(附解题思路)

  1. 将一个表自连接后再分别外接两个表

先自连接,然后分别对应自连接的两个表进行外连接,对应连接点是外连接的ON连接条件。将一个表自连接两次,解决调度问题

SELECT  ta.Anum FirstNum, ta.Acom FirstCom, ta.Bname Transfer, tc.Cnum SecondNum, tc.Ccom SecondCom
FROM(
SELECT  a.num Anum, a.company Acom, a.stop Astop, b.num Bnum, b.company Bcom, b.stop Bstop, stopb.name Bname
FROM route a JOIN route b ON(a.company = b.company && a.num = b.num)JOIN stops stopb ON (b.stop=stopb.id)WHERE (a.stop = (SELECT stops.id FROM stops WHERE name = 'Craiglockhart'))
)AS ta,(
SELECT  a.num Anum, a.company Acom, a.stop Astop, b.num Bnum, b.company Bcom, b.stop Bstop, c.num Cnum, c.company Ccom, c.stop Cstop
FROM route a JOIN route b ON(a.company = b.company && a.num = b.num)JOIN route c ON(b.company = c.company && b.num = c.num)WHERE (c.stop = (SELECT stops.id FROM stops WHERE name = 'Lochend')) && (a.stop = b.stop)
)AS tcWHERE ta.Bstop = tc.Bstop
GROUP BY FirstNum, FirstCom, Transfer, SecondNum, SecondCom

sqlzoo练习答案相关推荐

  1. 28、SQLZOO 练习答案 SELECT names

    SQLZOO 练习答案 SELECT names篇 www.sqlzoo.net 此教程使用LIKE運算子來檢查國家名字,我們會在world表格中運用SELECT語句: name:國家名稱 conti ...

  2. SQLZOO练习题答案参考(全)

    自学SQL将近一年,比较有名的SQL题库都尝试刷过,目前SQLZOO.XUESQL.牛客.Hackerrank初阶中阶.炼码基本刷完,Hackerrank高阶.Leetcode.SQL_intern仍 ...

  3. SQLZOO JOIN答案

    链接:https://sqlzoo.net/wiki/The_JOIN_operation 1. SELECT matchid,player FROM goal WHERE teamid='GER' ...

  4. SQLZOO练习答案(一):SELECT names/zh

    name continent Afghanistan Asia Albania Europe Algeria Africa Andorra Europe Angola Africa .... name ...

  5. [转]信息安全相关理论题(三)

    21.静态分析是运行程序后进行调试? A. 对 B. 错 您的答案: 标准答案: B 22.安卓反编译后会出现$符号字节码表示是匿名内部类? A. 对 B. 错 您的答案: 标准答案: A 23.反编 ...

  6. sqlzoo 答案全集

    文章目录 官网地址 答案导航 0. SELECT basics 基础 [参考答案](https://blog.csdn.net/q370835062/article/details/82950445) ...

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

    一:SELECT(http://www.sqlzoo.cn/1.htm)     1a. 查看关于bbc表的详细说明         SELECT name, region, population F ...

  8. sqlzoo答案参考(全)

    本文是作者在做完SQLZOO Tutorial练习后整理的答案,仅供参考. SQLZOO目录 SELECT basics SELECT from world SELECT from nobel SEL ...

  9. SQLZOO答案全部 Welcome to SQL Zoo

    1.SELECT basics                            (答案在链接里) 2.SELECT from WORLD Tutorial    (答案在链接里) 3.SELEC ...

最新文章

  1. 2006鄂土整项目精神
  2. 什么是Web Worker?
  3. Ethernet/IP 学习笔记四
  4. Android之如何实现阿拉伯版本(RTL)的recycleView的网格布局
  5. Netty Java快速指南
  6. 【C++深度剖析教程5】C++中类的静态成员函数
  7. php向下滑动,js如何判断鼠标滚轮是向下还是向上滚动
  8. linux恢复桌面,ubuntu恢复unity桌面
  9. eclipse导入wsdl文件_eclipse生成wsdl文件
  10. rk3399pro添加ALC5640音频配置
  11. printf() 输出数据格式汇总
  12. 加拿大大学计算机研究生专业排名,加拿大公立大学计算机专业研究生排名2013...
  13. unity不规则点击_【Unity游戏开发】UGUI不规则区域点击的实现
  14. WEB端工程环境安装
  15. volatility内存取证分析与讲解(持续更新)
  16. [转] 当猫爱上蝴蝶
  17. 小程序中如何关注公众号
  18. java中float、double和BigDecimal的精度问题(fastjson、Jackson以及实例化的方式)
  19. Maya Xgen交互式毛发的导出导入,用于其他项目
  20. 钙钛矿在太阳能电池领域的研究进展

热门文章

  1. 安卓手机微信记录还能找回吗
  2. 最新麦芒装饰装修小程序源码Ver3.2.59+全开源代码
  3. 【Java核心技术卷】静态语言和动态语言对比
  4. oracle11gr1怎么打开,oracle_11g_R1_安装教程
  5. matlab之矩阵乘法与点乘
  6. html单页面js完成表数据库自动生成带注释的java实体类和简单的增删改查sql
  7. Java读取excel的方式,一篇文章看懂(详细)
  8. php显示服务器域名,php获取服务器域名
  9. vm虚拟机安装ubuntu12.04配置安卓虚拟机
  10. 如果有一天: 你不再寻找爱情,只是去爱;你不再渴望成功,只是去做;你不再追求成长,只是去修;一切才真正开始! —— 纪伯伦