SQLZoo刷题系列 4

SUM and COUNT

刷题网站SQLZoo,刷题语言MySQL

知识点

 这套题目需要使用:SUM, Count, MAX, DISTINCT, GROUP BY 和 ORDER BY。其中count、sum、max这些函数属于聚合函数,除了上述函数,聚合函数还有avg、min,一共五个函数。
 聚合函数不能直接跟在where后,聚合函数是针对结果集进行的,但是where条件并不是在查询出结果集之后运行,所以主函数放在where语句中,会出现错误。如果我们想要使用聚合函数作为筛选条件的话,可以使用 HAVING 。如下图,可以将语句修改为select region,sum(area) from bbc group bt region having sum(area) > 15000000
在这里插入图片描述

1. SUM

 SUM 函数返回数值列的总数(总额),语法是SELECT SUM(column_name) FROM table_name

2. Count

 COUNT() 函数返回匹配指定条件的行数(空值不计),语法为:SELECT COUNT(column_name) FROM table_name

3. MAX

 MAX 函数返回一列中的最大值(NULL 值不包括在计算中),语法为:SELECT MAX(column_name) FROM table_name

4. DISTINCT

 关键词 DISTINCT 用于返回唯一不同的值,语法为SELECT DISTINCT 列名称 FROM 表名称

5. ORDER BY

 ORDER BY 语句用于根据指定的列对结果集进行排序,默认按照升序对记录进行排序,如果希望按照降序对记录进行排序,可以使用 DESC 关键字。语法是:SELECT column_name FROM table_name ORDER BY column_name

6. GROUP BY

 GROUP BY就是分组。必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段。
GROUP BY 和 ORDER BY的区别

7. HAVING

 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。具体用法为:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value

题目

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.

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748283174112960000000
AlgeriaAfrica238174137100000188681000000
AndorraEurope468781153712000000
AngolaAfrica124670020609294100990000000

1 Total world population

Show the total population of the world.

world(name, continent, area, population, gdp)
SELECT SUM(population) FROM world

2 List of continents

List all the continents - just once each.

select distinct continent from world

3 GDP of Africa

Give the total GDP of Africa

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

4 Count the big countries

How many countries have an area of at least 1000000

select count(name) from world
 where area > 1000000

5 Baltic states population

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

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 continents

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

select continent from world 
  group by continent
  having sum(population)>100000000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值