World Country Profile: Aggregate functions

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.

name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
...

1、Total world population

获取总人口。

Show the total population of the world.

SELECT SUM(population)
FROM world;

2、Lists of continent

List all the continents - just once each.

select DISTINCT(continent)
from world;

解题思路:distinct去重

3、GDP of Africa

获取非洲的gdp。

Give the total GDP of Africa

select sum(gdp)
from world
where continent='Africa';

4、count the big countries

计算有多少个国家,面积大于1000000

How many countries have an area of at least 1000000

select count(name)
from world
where area >=1000000;

5、Baltic states population

获取'Estonia', 'Latvia', 'Lithuania'的总人口。

What is the total population of ('Estonia', 'Latvia', 'Lithuania')

SELECT SUM(population)
FROM world
WHERE name IN('Estonia','Latvia','Lithuania');

6、Counting the countries of each continent

根据大洲分组,获取每个大洲还有国家的数量。

For each continent show the continent and number of countries.

select continent,count(name)
from world
group by continent;

7、Counting big countries in each continent

根据大洲分组,获取人口超过1000万的大洲名称,以及国家数量。

For each continent show the continent and number of countries with populations of at least 10 million.

select continent,count(name)
from world
where population>=10000000
group by continent;

8、counting big countries

获取总人口超过1亿的大洲名称。

List the continents that have a total population of at least 100 million.

select continent
from world
group by continent
having sum(population)>=100000000;

SQLZOO练习四--SUM and COUNT(聚合函数)的更多相关文章

  1. count()聚合函数正确用法

    count()聚合计算 count()是聚合函数,对于返回的结果集,一行行地判断,累计值加1,最后返回累计值,count(*).count(主键ID)和count(1)表示返回满足条件的结果集的总行数 ...

  2. mysql/mariadb学习记录——查询3(AVG、SUM、COUNT)函数

    AVG() 求平均数函数: //求emp表中的sal属性的平均值 mysql> select avg(sal) as salAverage from emp; +-------------+ | ...

  3. sqlzoo练习答案--SUM and COUNT

    World Country Profile: Aggregate functions This tutorial is about aggregate functions such as COUNT, ...

  4. Django ORM 多对多操作 使用聚合函数和分组 F查询与Q查询

    创建表 # models.py form django.db import models class Book(models.Model): # 表名book,django会自动使用项目名+我们定义的 ...

  5. MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Sum, Aggregate, Avg

    开篇介绍 SSAS 分析服务中记录了大量的聚合值,这些聚合值在 Cube 中实际上指的就是度量值.一个给定的度量值可能聚合了来自事实表中上千上万甚至百万条数据,因此在设计阶段我们所能看到的度量实际上就 ...

  6. sqlserver的over开窗函数(与排名函数或聚合函数一起使用)

    首先初始化表和数据 create table t_student(   Id INT,   Name varchar(),   Score int,   ClassId INT ); insert i ...

  7. 第08章 MySQL聚合函数

    第08章 MySQL聚合函数 我们上一章讲到了 SQL 单行函数.实际上 SQL 函数还有一类,叫做聚合(或聚集.分组)函数,它是对一组数据进行汇总的函数,输入的是一组数据的集合,输出的是单个值. 1 ...

  8. TSQL 聚合函数忽略NULL值

    max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null.count 聚合函数忽略null值,如果聚合列都是null ...

  9. 一.oracle的SQL中group by使用的情况(与聚合函数的关系)

    SELECT r.industry_1,r.industry_2,r.agent_id,r.agent_name,COUNT(DISTINCT r.customer_name_a)数据总量,COUNT ...

随机推荐

  1. Java汽车租赁系统[源码+数据库]

    系统名称 Java汽车租赁系统   (源码在文末) 系统概要 汽车租赁系统总共分为两个大的模块,分别是系统模块和业务模块.其中系统模块和业务模块底下又有其子模块. 功能模块 一.业务模块 1.客户管理 ...

  2. 交换机做节点的vlan划分

    基础准备 准备一台个人pc,两台物理服务器和一台三层交换机,以及网线若干. ip地址规划如下: 主机名 IP 控制节点 192.168.100.0 计算节点 192.168.200.0 个人pc 19 ...

  3. 劳动节快乐!手写个核心价值观编码工具 - Python实现

    前言 今天是五一劳动节,祝各位无产阶级劳动者节日快乐! 然后来整活分享一些有趣的东西~ 这个小工具是我大学时做着玩的,对于各位接班人来说,12个词的核心价值观这东西,大家都非常熟悉了,这工具可以实现将 ...

  4. 简单易懂的 Go 泛型使用和实现原理介绍

    原文:A gentle introduction to generics in Go by Dominik Braun 万俊峰Kevin:我看了觉得文章非常简单易懂,就征求了作者同意,翻译出来给大家分 ...

  5. 【翻译】ScyllaDB数据建模的最佳实践

    文章翻译自Scylla官方文档:https://www.scylladb.com/2019/08/20/best-practices-for-data-modeling/ 转载请注明出处:https: ...

  6. GIL与普通互斥锁区别,死锁现象,信号量,event事件,进程池与线程池,协程

    GIL与普通互斥锁区别 GIL锁和互斥锁的异同点 相同: 都是为了解决解释器中多个线程资源竞争的问题 异: 1.互斥锁是Python代码层面的锁,解决Python程序中多线程共享资源的问题(线程数据共 ...

  7. zabbix 线路质量监控自定义python模块(Mysql版),多线程(后来发现使用协程更好)降低系统消耗

    之前零零碎碎写了一些zabbix 线路监控的脚本,工作中agnet较多,每条线路监控需求不一致,比较杂乱,现在整理成一个py模块,集合之前的所有功能 环境 python3.6以上版本,pip3(pip ...

  8. windows 存储和切换 ip 配置

    我的虚拟机用的是桥接模式,在公司使用时设置的是静态 ip,但网段和家里面的不一样,就导致在公司和家里,我需要频繁修改 ipv4 的配置以适应不同的网络环境 Simple-IP-Config 工具解决了 ...

  9. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  10. 89. Gray Code - LeetCode

    Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...