
本文中主要是对sum and count进行了一个小测,熟悉
SQL中聚合函数的使用

欧洲所有国家的总人口
select sum(population)
from bbc
where region='Europe';统计人口小于150000的国家总数
select count(name)
from bbc
where population < 150000;列出SQL中aggregate函数,返回的是单一结果的函数
AVG(), COUNT(), CONCAT(), FIRST(), LAST(), MAX(), MIN(), SUM()select region, sum(area)
from bbc
where sum(area) > 15000000 -- 错误的写法
group by region;原因:where无法对区域总和进行分组,需要使用having来过滤行
正确写法:
select region, sum(area)
from bbc
group by region
having sum(area) > 15000000; --使用having进行过滤分组求解3个国家的平均人口数
select avg(population)
from bbc
where name in ('Poland', 'Germany', 'Denmark')显示每个region的平均人口密度
select region, sum(population)/sum(area) as density
from bbc
group by region;显示人口最多国家的人口密度
select name, population/area as density
from bbc
where population = (select max(population)
from bbc); -- 子查询中现将最大的人口数的国家选出来select region, sum(area)
from bbc
group by region
having sum(area) <= 20000000;region的人口总数2000000的过滤掉