SQL练习9月10



-- 显示世界人口总和
SELECT SUM(Population) FROM bbc b;


-- 列出所有的地区,每个地区只显示一次
SELECT Region FROM bbc GROUP BY Region;
SELECT DISTINCT  Region FROM bbc ;


-- 显示GDP超过所有Africa国家总和的国家
SELECT *FROM bbc b WHERE b.gdp>(SELECT SUM(gdp) FROM bbc bb WHERE bb.Region='Africa');




-- 显示每个地区及该地区国家总数
SELECT b.Region,COUNT(*)FROM bbc b GROUP BY  b.Region;


-- 显示每个地区的总人口数和总面积,以总人口排序
SELECT b.Region,SUM(b.population),SUM(b.contryArea) FROM bbc  b GROUP BY b.Region ORDER BY SUM(b.population)DESC;


-- 显示每个地区以及该地区国家总人口数不少于10000000的国家总数
SELECT b.Region , COUNT(*) FROM bbc b GROUP BY b.Region HAVING SUM(b.population)>10000000;


-- 列出人口总数不少于100000000的地区
SELECT b.Region ,SUM(b.population) num FROM bbc b GROUP BY b.Region HAVING SUM(population)>100000000;


-- 显示欧州人口总数和总GDP
SELECT SUM(b.population),SUM(b.gdp) FROM bbc b WHERE b.Region='Europe';


-- 显示每个地区的总人口数和总面积,以总人口排序,仅显示那些面积超过1000000的地区
SELECT b.Region, SUM(b.population),SUM(b.contryArea)FROM bbc b GROUP BY b.Region HAVING SUM(b.contryArea)>1000000 ORDER BY SUM(b.population) DESC;


-- 获得到和‘Brazil’同属一个地区的所有国家
SELECT* FROM bbc b WHERE b.region=(
SELECT bc.region FROM bbc  bc WHERE  bc.contryName='Brazil'
) ;


-- 给出人口多于Russia的国家名称
SELECT *FROM bbc b WHERE b.population>(
SELECT bc.population FROM bbc bc WHERE bc.contryName='Russia'
);


-- 给出'India','Iran'所在地区的所有国家信息


SELECT * FROM bbc b WHERE b.region  IN(
SELECT region FROM bbc bc WHERE bc.contryName='India'OR contryName='Iran'
);


-- 给出人均gdp超过'United Kingdom'(英国)的欧州国家
SELECT * FROM bbc b WHERE b.region='Europe' AND
b.gdp/population>(
SELECT bc.gdp/bc.population FROM bbc bc WHERE bc.contryName='United Kingdom'
)


-- 给出人口比'Canada'多但少于Algeria的国家的信息
SELECT * FROM bbc b WHERE b.population>(
SELECT bc.population FROM bbc bc WHERE bc.contryName='Canada' 
)AND b.population<(
SELECT bc.population FROM bbc bc WHERE bc.contryName='Algeria'
)


-- 给出gdp比任何欧州国家都多的国家
SELECT * FROM bbc b WHERE b.gdp>(
SELECT MAX(gdp) FROM bbc bc WHERE bc.region='Europe'
)


-- 给出每个地区人口最大的国家
SELECT *FROM bbc b WHERE b.population IN (
SELECT MAX(bc.population) FROM bbc bc GROUP BY bc.region
)


-- 给出地区中所有国家的人口总数为0的地区
SELECT b.region FROM bbc b GROUP BY b.region HAVING SUM(b.population)=0


-- 有些国家的人口数比她的周边国家要多三倍,请列出这些国家和地区。
SELECT *FROM bbc b WHERE  b.population>4*(
SELECT MAX(bc.population) FROM bbc bc WHERE b.region=bc.region
AND b.contryName !=bc.contryName
)

你可能感兴趣的:(SQL练习9月10)