首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >sqlzoo练习7-聚合

sqlzoo练习7-聚合

作者头像
皮大大
发布2021-03-02 16:33:19
发布2021-03-02 16:33:19
6590
举报

sqlzoo练习7

主要涉及到的知识点是聚合函数sum and count

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

  • sum求和
  • count统计数量
  • max、min 求出最值
  • distinct去重功能

练习

  1. Show the total population of the world.
代码语言:javascript
复制
select sum(population) from world;
  1. List all the continents - just once each.

列出每个不同的洲,使用distinct去重

代码语言:javascript
复制
select distinct(continent) from world;
  1. Give the total GDP of Africa

计算非洲的总gdp,sum求和

代码语言:javascript
复制
select sum(gdp) from world where continent='Africa';
  1. How many countries have an area of at least 1000000

统计count多少个国家的面积大于1000000

代码语言:javascript
复制
select count(name) from world
 where area > 1000000;
  1. What is the total population of (‘Estonia’, ‘Latvia’, ‘Lithuania’)

3个国家的总人口sum

代码语言:javascript
复制
select sum(population) from world
where name in ('Estonia', 'Latvia', 'Lithuania');
  1. For each continent show the continent and number of countries.

每个地区continent有多少个国家count

代码语言:javascript
复制
select continent, count(name)  -- 统计总数
from world
group by continent;  -- 地区分组
  1. For each continent show the continent and number of countries with populations of at least 10 million.

加上where条件再进行分组

代码语言:javascript
复制
select continent, count(name)
from world
where population >= 10000000  -- 先用where进行筛选
group by continent;
  1. List the continents that have a total population of at least 100 million.

having是对分组之后的结果进行筛选

代码语言:javascript
复制
select continent
from world
group by continent  -- 先分组再进行筛选
having sum(population) >= 100000000;

Group By and Having

select子句顺序
  1. select
  2. from
  3. where
  4. group by
  5. having
  6. order by
习题
  1. For each continent show the number of countries

统计每个洲中国家的数量

  • 洲分组
  • 统计数量count
代码语言:javascript
复制
select continent, count(name)
from world
group by continent;  -- 分组
  1. For each continent show the total population

统计每个洲的人口总数

  • 洲分组
  • 统计总数sum
代码语言:javascript
复制
select continent, sum(population)  -- 人口求和
from world
group by continent;
  1. For each relevant continent show the number of countries that has a population of at least 200000000.

where语句在group by之前

  • 每个地区的国家总数
  • 人口需要大于20000000
代码语言:javascript
复制
select continent,count(name)
from world
where population > 20000000
group by continent;
  1. Show the total population of those continents with a total population of at least half a billion.

The HAVING clause is tested after the GROUP BY

代码语言:javascript
复制
select continent, sum(population)   -- 统计总人口
from world
group by continent
having  sum(population) > 500000000;   -- 总人口满足的条件
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-1-15,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • sqlzoo练习7
  • 练习
  • Group By and Having
    • select子句顺序
    • 习题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档