欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

对嵌套SELECT语句的练习:SELECTwithinSELECTTutorial

程序员文章站 2021-12-02 10:57:02
找到了一个很好的在线练习sql的网站,本节是对嵌套select语句的练习,以下是这部分的答案和解析。 this tutorial looks at how we can use select sta...

找到了一个很好的在线练习sql的网站,本节是对嵌套select语句的练习,以下是这部分的答案和解析。

this tutorial looks at how we can use select statements within select statements to perform more complex queries.

name continent area population gdp
afghanistan asia 652230 25500100 20343000000
albania europe 28748 2831741 12960000000
algeria africa 2381741 37100000 188681000000
andorra europe 468 78115 3712000000
angola africa 1246700 20609294 100990000000
...

1、list each countrynamewhere thepopulationis larger than that of 'russia'.

world(name, continent, area, population, gdp)
 select name from world
  where population > (select population from world
                       where name='russia')

重点:找到 'russia' 的 population 然后进行对比  
'russia' 的 population:select population from world where name='russia' 
对比—: population >                 

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 thenameandcontinentof countries in the continents containing eitherargentinaoraustralia. 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 

用到了两次 in

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.

decimal placespercent symbol%
select name, concat(round(100 * population/(select population from world where name='germany')),'%') from world 
 where continent='europe'

6、which countries have a gdp greater than every country in europe [give thenameonly.] (some countries may have null gdp values)

select name from world 
 where gdp > all(select gdp from world 
                  where gdp>0 
                    and continent='europe')

7、find the largest country (by area) in each continent, show the continent, thenameand thearea:

select continent, name, area from world x
 where x.area >= all(select area from world y
               where y.continent = x.continent
           and area>0)
在相同的 continent 中对 area 进行对比

对嵌套SELECT语句的练习:SELECTwithinSELECTTutorial

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

select continent, name from world x 
 where x.name = (select y.name from world y 
                  where y.continent = x.continent 
                  order by name 
                  limit 1) 

对嵌套SELECT语句的练习:SELECTwithinSELECTTutorial

9、find the continents where all countries have a population <= 25000000. then find the names of the countries associated with these continents. showname,continentandpopulation.

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

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 x 
 where x.population/3 >= all(select population from world y 
                              where x.continent=y.continent                                   
                                and x.name!=y.name  
                                add y.population>0) 
选取的结果不能和自身进行对比!