1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > sqlzoo刷题——SUM and COUNT(聚合函数)

sqlzoo刷题——SUM and COUNT(聚合函数)

时间:2023-08-30 21:32:16

相关推荐

sqlzoo刷题——SUM and COUNT(聚合函数)

前言

本文章记录sqlzoo刷题过程以及解题思路,每个小节不仅包含练习,还有选择题(quiz)部分的实现思路

网址:sqlzoo_SUM and COUNT

一、代码练习部分

展示世界的總人口。

查询结果列——

条件——

查询逻辑——

SELECT sum(population)FROM world

列出所有的洲份, 每個只有一次。

查询结果列——

条件——group by分组之后每个元素都聚合到一行了

查询逻辑——先分组,再选择显示洲份

select continentfrom worldgroup by continent

找出非洲(Africa)的GDP總和。

查询结果列——gdp_sum

条件——国家:非洲

查询逻辑——先按洲际分组,选择非洲的行,计算gdp总和并显示

select sum(gdp) as gdp_totalfrom worldwhere continent='Africa'group by continent

有多少個國家具有至少百萬(1000000)的面積。

查询结果列——国家数:count(国家名)

条件——面积:>=1000000

查询逻辑——选择面积>=1百万的行,显示计数

select count(name)from worldwhere area>=1000000

(‘France’,‘Germany’,‘Spain’)(“法國”,“德國”,“西班牙”)的總人口是多少?

查询结果列——总人口(sum(population))

条件——国家:法,德,西=国家in(法德西)

查询逻辑——先查询出法德西的行,再计算三者的总和

selectsum(population) as popu_totalfrom worldwhere name in ('France','Germany','Spain')

對於每一個洲份,顯示洲份和國家的數量。

查询结果列——国家,数量

条件——每一个洲份=需要先聚合洲份

查询逻辑——先聚合洲份,再计算每个洲份分组内包含的国家数量,显示洲份和其数量

select continent,count(name)as totalfrom world group by continent

對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目。

查询结果列——洲份,国家数目

条件——

1、按洲份分组

2、洲内人口>=1千万的国家(计数)

查询逻辑——先选择人口>=1千万的行,按洲份分组,显示洲份,国家数目

select continent,count(name)from worldwhere population >10000000group by continent

列出有至少100百萬(1億)(100,000,000)人口的洲份。

查询结果列——洲份

条件——总人口>1亿=sum(人口)>1亿。

查询逻辑——按照洲分组,计算每组的总人口,再筛选出其中>1亿的组,显示洲份。

值得注意的是使用聚合函数对于分组总体为对象选择条件的,要先group by分组,再having选择条件,而不是where+条件再group by分组。

select continentfrom worldgroup by continenthaving sum(population)>100000000

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

查询结果列——

条件——

查询逻辑——

在这里插入代码片

二、测试部分(选择题)

Select the statement that shows the sum of population of all countries in ‘Europe’

查询结果列——总人口(sum)

条件——属于欧洲的国家

查询逻辑——先选择欧洲国家的全部行,再计算总人口,显示总人口列

select sum(population)from bbcwhere region='Europe'

Select the statement that shows the number of countries with population smaller than 150000

查询结果列——国家数(count)

条件——人口<15万

查询逻辑——先查出人口小于15万的行,对国家进行计数,显示计数结果

select count(name)from bbcwhere population<150000

Select the list of core SQL aggregate functions

聚合函数的定义:对一对数值进行计算

在这里插入代码片

Select the result that would be obtained from the following code:

SELECT region, SUM(area)FROM bbc WHERE SUM(area) > 15000000 GROUP BY region

聚合函数作为条件时,要放在having中

Select the statement that shows the average population of ‘Poland’, ‘Germany’ and ‘Denmark’

查询结果列——平均人口(avg)

条件——三个国家的平均人口(in )

查询逻辑——先查询出这三个国家的行,再计算平均人口,选择显示

select avg(population)from bbc where name in ('Poland', 'Germany' and 'Denmark')

Select the statement that shows the medium population density of each region

查询结果列——人口密度(人口/区域)

条件——按区域分组

查询逻辑——每个地区的平均区域人口(每平米有多少个人)

注意:

1、人口密度=人口/面积。

2、一个地区的人口密度与一个国家人口密度计算的区别

select sum(population)/sum(area)as densityfrom bbcgroup by region

Select the statement that shows the name and population density of the country with the largest population

查询结果列——国家名,人口密度

条件——人口最多的国家(聚合函数+子查询)

查询逻辑——查询中人口最大的行,显示国家,地区,人口密度。

select region,name,sum(population)/sum(area) as densityfrom bbc where population=(select max(population/area) from bbc)

总结

聚合函数主要是对一堆数值,以组为对象进行计算,如一堆数的总和,总数,平均,这堆数中的最大值,最小值等,应用对象只能是数值。

与聚合函数相关的主要是group by和having 的使用。作为结果的时候在 select中新增列;聚合作为条件的时候要放在group by之后,用having,不可以在where中使用聚合函数。如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。