SQL GROUP BY and SUM

前端 未结 2 1319
离开以前
离开以前 2020-12-21 03:32

List the continents with total populations of at least 100 million.

World Table

name         continent  area    population  gdp
Afghanistan  Asia       6522         


        
相关标签:
2条回答
  • 2020-12-21 04:19

    I'll give you a good framework for thinking through this question.

    Since there are multiple records with the same continent, we know we need GROUP BY. Once we do group by, we can use aggregate operations to get the sum, namely SUM. By using this aggregate operation, we can filter using the HAVING clause post group-by. If we wanted to filter pre-groupby, we would use the WHERE clause.

    SELECT continent FROM world GROUP BY continent HAVING SUM(population) > 
    100000000;
    
    0 讨论(0)
  • 2020-12-21 04:23
    SELECT   continent, SUM(population)
    FROM     world
    GROUP BY continent
    HAVING   SUM(population) >= 100000000
    
    0 讨论(0)
提交回复
热议问题