SQLZOO练习四--SUM and COUNT(聚合函数)

World Country Profile: Aggregate functions

This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.

namecontinentareapopulationgdp
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、Total world population

获取总人口。

Show the total population of the world.

SELECT SUM(population)
FROM world;

2、Lists of continent

List all the continents - just once each.

select DISTINCT(continent)
from world;

解题思路:distinct去重

3、GDP of Africa

获取非洲的gdp。

Give the total GDP of Africa

select sum(gdp)
from world
where continent='Africa';

4、count the big countries

计算有多少个国家,面积大于1000000

How many countries have an area of at least 1000000

select count(name)
from world
where area >=1000000;

5、Baltic states population

获取'Estonia', 'Latvia', 'Lithuania'的总人口。

What is the total population of ('Estonia', 'Latvia', 'Lithuania')

SELECT SUM(population)
FROM world
WHERE name IN('Estonia','Latvia','Lithuania');

6、Counting the countries of each continent

根据大洲分组,获取每个大洲还有国家的数量。

For each continent show the continent and number of countries.

select continent,count(name)
from world
group by continent;

7、Counting big countries in each continent

根据大洲分组,获取人口超过1000万的大洲名称,以及国家数量。

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、counting big countries

获取总人口超过1亿的大洲名称。

List the continents that have a total population of at least 100 million.

select continent
from world
group by continent
having sum(population)>=100000000;
posted @ 2022-05-13 12:25  徐若离  阅读(46)  评论(0编辑  收藏  举报