本文中主要是对sum and count进行了一个小测,熟悉
SQL
中聚合函数的使用
Sum and Count Quiz
练习
- Select the statement that shows the sum of population of all countries in ‘Europe’
欧洲所有国家的总人口
1 | select sum(population) |
- Select the statement that shows the number of countries with population smaller than 150000
统计人口小于150000的国家总数
1 | select count(name) |
- Select the list of core SQL aggregate functions
列出SQL中aggregate函数,返回的是单一结果的函数
1 | AVG(), COUNT(), CONCAT(), FIRST(), LAST(), MAX(), MIN(), SUM() |
- Select the result that would be obtained from the following code,根据代码选择结果
1 | select region, sum(area) |
- area总数大于15000000;(写法错误)
- 根据地区region进行分组
原因:where无法对区域总和进行分组,需要使用having来过滤行
正确写法:
1 | select region, sum(area) |
- Select the statement that shows the average population of ‘Poland’, ‘Germany’ and ‘Denmark’
求解3个国家的平均人口数
1 | select avg(population) |
- Select the statement that shows the medium population density of each region
显示每个region的平均人口密度
1 | select region, sum(population)/sum(area) as density |
- Select the statement that shows the name and population density of the country with the largest population
显示人口最多国家的人口密度
1 | select name, population/area as density |
- Pick the result that would be obtained from the following code
1 | select region, sum(area) |
- 先求出每个
region
的人口总数 - 再把人口总数小于等于
2000000
的过滤掉