美文网首页大数据学习+数据库知识
Sqlzoo 答案 SUM & COUNT & GROUP BY

Sqlzoo 答案 SUM & COUNT & GROUP BY

作者: kolibreath | 来源:发表于2020-03-07 14:37 被阅读0次

    第一部分 GROUP BY & HAVING

    For each continent show the number of countries:

    world(name, continent, area, population, gdp)

    SELECT continent, COUNT(name) FROM world GROUP BY continent
    

    For each continent show the total population:

    SELECT continent, SUM(population) FROM world GROUP BY continent
    

    WHERE and GROUP BY. The WHERE filter takes place before the aggregating function. For each relevant continent show the number of countries that has a population of at least 200000000.

    WHERE and GROUP BY. The WHERE filter takes place before the aggregating function. For each relevant continent show the number of countries that has a population of at least 200000000.
    

    GROUP BY and HAVING. The HAVING clause is tested after the GROUP BY. You can test the aggregated values with a HAVING clause. Show the total population of those continents with a total population of at least half a billion.

    SELECT continent, SUM(population)
      FROM world
     GROUP BY continent
    HAVING SUM(population)>500000000
    

    第二部分 SUM COUNT

    ![(https://img.haomeiwen.com/i4714178/ab6fbbcc1ff6a436.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    Show the total population of the world.

    SELECT SUM(population)
    FROM world
    

    List all the continents - just once each.

    SELECT distinct continent FROM world
    

    Give the total GDP of Africa

    SELECT SUM(gdp) FROM world WHERE continent  = 'Africa'
    

    How many countries have an area of at least 1000000

    SELECT COUNT(name) FROM world WHERE area >= 1000000
    

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

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

    For each continent show the continent and number of countries.

    SELECT continent, COUNT(name) FROM world GROUP BY 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
    

    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 & COUNT & GROUP BY

        本文链接:https://www.haomeiwen.com/subject/dguqdhtx.html