sqlzoo答案参考

select basics

1.Modify it to show the population of Germany

select population
from world
where name='Germany'

2.show the name and the population for 'Sweden', 'Norway' and 'Denmark'.

select name, population 
from world
where name in ('Sweden', 'Norway', 'Denmark')

3.Modify it to show the country and the area for countries with an area between 200,000 and 250,000.

select name,area
from world
where 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.

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

4. Show the name and population in millions for the countries of the continent 'South America'. Divide the population by 1000000 to get population in millions.

select name,population/1000000
from world
where continent='South America'

5. Show the name and population for France, Germany, Italy

select name,population
from world
where name in ('France','Germany','Italy')

6. Show the countries which have a name that includes the word 'United'

select name
from world
where name like ('%United%')

7. Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.

Show the countries that are big by area or big by population. Show name, population and area.

select name,population,area
from world
where area>3000000 or population>250000000

8. Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.

Australia has a big area but a small population, it should be included.

Indonesia has a big population but a small area, it should be included.

China has a big population and big area, it should be excluded.

United Kingdom has a small population and a small area, it should be excluded.

select name,population,area
from world
where (population>250000000 or area>3000000) 
and not (population>250000000 and area>3000000)

9. Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.

For South America show population in millions and GDP in billions both to 2 decimal places.

select name,round(population/1000000,2),round(gdp/1000000000,2)
from world
where continent='South America'

10. Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.

Show per-capita GDP for the trillion dollar countries to the nearest $1000.

select name,round(gdp/population,-3)
from world
where gdp>=1000000000000

11. Greece has capital Athens.

Each of the strings 'Greece', and 'Athens' has 6 characters.

Show the name and capital where the name and the capital have the same number of characters.

You can use the LENGTH function to find the number of characters in a string

select name,capital
from world
where length(name)=length(capital)

12. The capital of Sweden is Stockholm. Both words start with the letter 'S'.

Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.

select name,capital
from world
where left(name,1)=left(capital,1) and name<>capital

13. Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count because they have more than one word in the name.

Find the country that has all the vowels and no spaces in its name.

You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.

The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'

select name
from world
where name not like '% %' and name like '%a%' and name like '%o%'
and name like '%e%' and name like '%i%' and name like '%u%'

SELECT from Nobel Tutorial

  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 yr>=2000 and subject='Peace'

5. Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.

select yr,subject,winner
from nobel
where yr between 1980 and 1989 and subject='Literature'

6. Show all details of the presidential winners: Theodore Roosevelt, Woodrow Wilson, Jimmy Carter, Barack Obama

select * 
from nobel
where winner in ('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter','Barack Obama')

7. Show the winners with first name John

select winner
from nobel
where winner like 'John %'

8. Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.

select yr,subject,winner
from nobel
where (yr=1984 and subject='Chemistry') or (yr=1980 and subject='Physics')

9. Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine

select yr,subject,winner
from nobel
where yr=1980 and (subject<>'Chemistry' and subject<>'Medicine')

10. Show year, subject, and name of people who won a 'Medicine' prize in an early year (before 1910, not including 1910) together with winners of a 'Literature' prize in a later year (after 2004, including 2004)

select yr,subject,winner
from nobel
where (subject='Medicine' and yr<1910) or (subject='Literature' and yr>=2004)

11. Find all details of the prize won by PETER GRÜNBERG

select *
from nobel
where winner like 'peter gr%nberg'

12. Find all details of the prize won by EUGENE O'NEILL

SELECT *
FROM nobel
WHERE winner = 'Eugene O''Neill'

13. List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.

select winner,yr,subject
from nobel
where winner like 'sir%' order by yr desc,winner

14. Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

The expression subject IN ('Chemistry','Physics') can be used as a value

- it will be 0 or 1.

select winner, subject
from nobel
where yr=1984
order by subject in ('Chemistry','Physics'),subject,winner

SELECT within SELECT Tutorial

  1. List each country name where the population is larger than that of 'Russia'.
select name
from world
where population>(select population 
                  from world
                  where name='Russia')

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

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 continent from world
                    where name in ('Argentina','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. Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

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

注:上面的concat(region,name)将region和name沾合在一起显示

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 continent='Europe' and gdp>0)

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

select continent,name,area
from world as x
where x.area=(select max(y.area)
              from world as y
              where x.continent=y.continent)

注:x,y表示用到了2次world表

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

select continent,name
from world x
where x.name<=all(select name from world y where x.continent=y.continent)
order by name  

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

select name,continent,population
from world as x
where 25000000>=all
(select population from world y where x.continent=y.continent)

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 name,continent
from world as x
where population/3>=all(select population from world y where 
 x.continent=y.continent and x.name!=y.name)

SUM and COUNT

  1. Show the total population of the world.
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='Afri

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 having count(name)

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 having count(name)

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

The JOIN operation

  1. 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. Show id, stadium, team1, team2 for just game 1012

select id,stadium,team1,team2
from game
where id=1012

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

select player,teamid,stadium,mdate
from goal,game
where goal.matchid=game.id and teamid='GER'

4.Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

select team1,team2,player
from game,goal
where game.id=goal.matchid and player like 'Mario%'

5.Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

select player,teamid,coach,gtime
from goal,eteam
where eteam.id=goal.teamid and gtime<=10

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

select mdate,teamname
from game,eteam
where game.team1=eteam.id and coach='Fernando Santos'

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

select player
from goal,game
where goal.matchid=game.id and stadium='National Stadium, Warsaw'

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

select distinct player
from goal,game
where goal.matchid=game.id and (team1='GER' or team2='GER') and teamid<>'GER'

9.Show teamname and the total number of goals scored.

select teamname,count(teamid)
from goal,eteam
where goal.teamid=eteam.id
group by teamname

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

select stadium,count(*)
from goal,game
where goal.matchid=game.id
group by stadium

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

select matchid,mdate,count(*)
from goal,game
where goal.matchid=game.id and (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(*)
from goal,game
where goal.matchid=game.id and 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.



More JOIN operations

1.List the films where the yr is 1962 [Show id, title]

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 id, title 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'.Use movieid=11768

select name
from actor inner join casting on actor.id=casting.actorid and movieid=11768

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

select actor.name
from actor
join casting
on casting.actorid=actor.id
join movie
on movie.id=casting.movieid
where title='Alien'

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

select title
from movie
join casting
on casting.movieid=movie.id
join actor
on actor.id=casting.actorid
where 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
join casting
on casting.movieid=movie.id
join actor
on casting.actorid=actor.id
where name='Harrison Ford' and ord<>1

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

select movie.title,actor.name
from movie
join casting
on casting.movieid=movie.id
join actor
on casting.actorid=actor.id
where yr=1962 and ord=1

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

select movie.yr,count(*)
from movie
join casting
on casting.movieid=movie.id
join actor
on casting.actorid=actor.id
where actor.name='John Travolta'
group by movie.yr
having count(*)>2

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

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


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

select name
from actor
join casting
on casting.actorid=actor.id
where ord=1 
group by name
having count(name)>=30
order by name

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

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

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

select a.name
from (select movie.*
      from movie
      join casting
      on casting.movieid=movie.id
      join actor
      on casting.actorid=actor.id
      where name='Art Garfunkel') as m
join (select actor.*,casting.movieid as movieid
      from actor
      join casting
      on casting.actorid=actor.id
      where name!='Art Garfunkel') as a
on m.id=a.movieid

Using Null

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

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.

注:在表中至少一个匹配时,inner join关键字返回行

select teacher.name,dept.name
from teacher inner join dept on dept.id=teacher.dept

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

注:会从左表那里返回所有行,即使在又表中没有匹配的行

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

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

注:会从又表那里返回所有行,即使在左表没有匹配的行

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

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'

coalesce语法

select teacher.name,coalesce(mobile,'07986 444 2266')
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 teacher.name,coalesce(dept.name,'None')
from teacher left join dept on dept.id=teacher.dept

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

select count(teacher.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 dept.id=teacher.dept
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.

注:case用法

select name,
case when dept in(1,2) then 'Sci'
     else 'Art'
end
from teacher

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 name,
case when dept in (1,2) then 'Sci'
     when dept=3 then 'Art'
     else 'None'
end
from teacher

Self join

1. How many stops are in the database.

select count(id)
from stops

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

select stops.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
inner join route on route.stop=stops.id
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 in (149,53)
group by company,num
having 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 as a
join route as 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 as a
join route as b
on (a.company=b.company and a.num=b.num)
join stops as stopa
on a.stop=stopa.id
join stops as stopb
on b.stop=stopb.id
where stopa.name='Craiglockhart' and stopb.name='London Road'

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 as a
join route as b
on a.num=b.num and a.company=b.company
where a.stop=115 and b.stop=137

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

select a.company,b.num
from route as a
join route as b
on a.company=b.company and a.num=b.num
join stops as x
on a.stop=x.id
join stops as y
on b.stop=y.id
where x.name='Craiglockhart' and y.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 x.name,a.company,a.num
from route as a
join route as b
on a.company=b.company and a.num=b.num
join stops as x
on a.stop=x.id
join stops as y
on b.stop=y.id
where y.name='Craiglockhart'

10. 待研究

  • 14
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值