sqlzoo:using group by and having

For each continent show the number of countries:

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.

SELECT continent, COUNT(name)
  FROM world
 WHERE population>200000000
 GROUP BY continent

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

 

posted @ 2019-03-19 17:39  litteyellowduck  阅读(225)  评论(0编辑  收藏  举报