4.SELECT within SELECT Tutorial

world表

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. 列出人口比 "俄罗斯 "多的每个国家名称。
SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')
  1. 显示欧洲人均GDP大于 "英国 "的国家。
SELECT name FROM world
  WHERE gdp/population >
     (SELECT gdp/population FROM world
      WHERE name='United Kingdom')
  and continent = 'Europe';
  1. 列出包含阿根廷或澳大利亚的各大洲中的国家名称和大洲。按国家名称排序。
select name,continent
 from world
  where continent in (select continent from world where name ='Argentina' or name ='Australia')
 order by name;
  1. 哪个国家的人口比加拿大多,但比波兰少?出示名称和人口。
select name, population from world
where population > (select population from world where name = 'Canada')
 and population < (select population from world where name = 'Poland');
  1. 德国(人口8000万)是欧洲国家中人口最多的国家。奥地利(人口850万)的人口占德国的11%。
    显示欧洲每个国家的名称和人口。显示人口占德国人口的百分比。
select name, concat(round(population/(select population from world where name = 'Germany ')*100,0),'%') as percentage from world
where continent = 'Europe';
  1. 哪些国家的国内生产总值高于欧洲所有国家?[只提供名称](有些国家的GDP值可能为NULL)。
select name from world
where gdp > all(SELECT gdp FROM world WHERE gdp >0 and continent = 'Europe');
  1. 找出各大洲最大的国家(按面积),显示大洲、名称和面积。
SELECT continent, name, area FROM world x
  WHERE area >= ALL
    (SELECT area FROM world y
        WHERE y.continent=x.continent
          AND area >0);
  1. 按字母顺序列出各大洲和排在前面的国家名称。
select continent,name
from world x
where name <= ALL
    (SELECT name FROM world y
        WHERE y.continent=x.continent);
  1. 找出所有国家人口<=25000000的大洲。然后找出与这些大洲相关的国家名称。显示名称、大陆和人口。
select name, continent, population
from world x
where 25000000 >=
    all(SELECT population FROM world y
        WHERE y.continent=x.continent);
  1. 一些国家的人口是其任何邻国(在同一大陆)人口的三倍以上。请说明这些国家和大陆。
select name, continent
from world x
where population >=
    all(SELECT 3*population FROM world y
        WHERE y.continent=x.continent and y.name <> x.name);
posted @ 2021-03-01 18:13  hj0612  阅读(50)  评论(0)    收藏  举报