Search code examples
sqlselectgroup-bysum

SQL GROUP BY and SUM


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

World Table

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    10009000990
...
...

I started with

SELECT continent FROM world WHERE ... and kind of got stuck here.

Not sure how I can leverage GROUP BY and SUM. I need to GROUP BY continent and SUM(population) some how but I am still learning how to put things together.

expected output

continent
Africa
Asia
Eurasia
Europe
North America
South America

Solution

  • SELECT   continent, SUM(population)
    FROM     world
    GROUP BY continent
    HAVING   SUM(population) >= 100000000