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. 如何改变函数内部 this 的指向

    一.函数内 this 的指向 1. this 的指向是当调用函数时确定的,调用的方式不同,this 的指向也就不同. 1.1 this 一般是指向调用者. 函数类型 this 的指向 普通函数 Win ...

  2. linux系统如何查看内核版本、操作系统版本等信息

    有时候需要查看linux系统的内核版本,可以有多种方法,方法如下:(下面以优麒麟系统为例) 方法1: 打开mate终端,在命令行输入以下命令: uname -a 运行效果如下: ​​​ 如果只查看内核 ...

  3. 面试官给我挖坑:rm删除文件之后,空间就被释放了吗?

    在Linux,你是不是曾经天真的以为,使用rm删除一个文件,占用的空间就释放了?事情可能不是常常如人意. 产生一个指定大小的随机内容文件 我们先看一下当前各个挂载目录的空间大小: $ df -h /d ...

  4. 《Mybatis 手撸专栏》第7章:SQL执行器的定义和实现

    作者:小傅哥 博客:https://bugstack.cn - <手写Mybatis系列> 一.前言 为什么,要读框架源码? 因为手里的业务工程代码太拉胯了!通常作为业务研发,所开发出来的 ...

  5. selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装

    今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...

  6. 找到占用CPU最高的Java线程

    一.找到java进程id jps查看当前运行的java进程id [root@localhost ~]# jps 18354 Jps 9381 Bootstrap 二.找到内存和CPU占用最高的线程pi ...

  7. 【leetcode】239. 滑动窗口最大值

    目录 题目 题解 三种解法 "单调队列"解法 新增.获取最大值 删除 代码 题目 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以 ...

  8. Yaml中特殊符号"| > |+ |-"的作用

    "|",保留每行尾部的换行符\n. ">",删除每行尾部的换行符\n,则看似多行文本,则在程序中会将其视为一行. include_newlines: | ...

  9. 【单片机】使用 sscanf 提取AT命令返回结果中的有效数据

    摘要:1. sscanf函数 sscanf是C标准库函数,用于从字符串中读取格式化输入. 头文件: #include <stdio.h>函数原型如下: int sscanf(const c ...

  10. ESP8266 系统环境搭建

    1. 前言 因为ESP8266/ESP32这个开发环境没少折腾,是真没见过这么难搞又不清晰的环境. 简单开发可以使用Arduino IDE ,这个平台还是挺好的.开发使用Arduino的函数库,很高效 ...