首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >sqlzoo练习9-sum-count-quiz

sqlzoo练习9-sum-count-quiz

作者头像
皮大大
发布2021-03-02 16:32:50
发布2021-03-02 16:32:50
6460
举报

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

Sum and Count Quiz

练习

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

欧洲所有国家的总人口

代码语言:javascript
复制
select sum(population)
from bbc
where region='Europe';
  1. Select the statement that shows the number of countries with population smaller than 150000

统计人口小于150000的国家总数

代码语言:javascript
复制
select count(name)
from bbc
where population < 150000;
  1. Select the list of core SQL aggregate functions

列出SQL中aggregate函数,返回的是单一结果的函数

代码语言:javascript
复制
AVG(), COUNT(), CONCAT(), FIRST(), LAST(), MAX(), MIN(), SUM()
  1. Select the result that would be obtained from the following code,根据代码选择结果
代码语言:javascript
复制
select region, sum(area)
from bbc
where sum(area) > 15000000  -- 错误的写法
group by region;
  • area总数大于15000000;(写法错误)
  • 根据地区region进行分组

原因:where无法对区域总和进行分组,需要使用having来过滤行

正确写法:

代码语言:javascript
复制
select region, sum(area)
from bbc
group by region
having sum(area) > 15000000;  --使用having进行过滤分组
  1. Select the statement that shows the average population of ‘Poland’, ‘Germany’ and ‘Denmark’

求解3个国家的平均人口数

代码语言:javascript
复制
select avg(population)
from bbc
where name in ('Poland', 'Germany', 'Denmark')
  1. Select the statement that shows the medium population density of each region

显示每个region的平均人口密度

代码语言:javascript
复制
select region, sum(population)/sum(area) as density
from bbc
group by region;
  1. Select the statement that shows the name and population density of the country with the largest population

显示人口最多国家的人口密度

代码语言:javascript
复制
select name, population/area as density
from bbc
where population = (select max(population)
                    from bbc);  -- 子查询中现将最大的人口数的国家选出来
  1. Pick the result that would be obtained from the following code
代码语言:javascript
复制
select region, sum(area)
from bbc
group by region
having sum(area) <= 20000000;
  • 先求出每个region的人口总数
  • 再把人口总数小于等于2000000的过滤掉

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-1-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Sum and Count Quiz
  • 练习
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档