HAVING 和 GROUP BY

HAVING and GROUP BY

我要解决的问题如下

For each continent show the continent and number of countries with populations of at least 10 million

世界table是这样的:

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

我的查询(未返回正确结果):

SELECT continent, COUNT(name)
FROM world
GROUP BY continent
HAVING sum(population) >= 10000000

返回正确结果的查询:

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

谁能告诉我为什么我的查询是错误的?

问题是关于国家,而不是大洲,所以你需要在聚合之前进行过滤。

您的查询版本正在回答:

How many countries are in continents whose population is greater than 10,000,000?

问题是:

How many countries in each continent have a population greater than 10,000,000?

这些是不同的问题。我也意识到,对于英语不流利的人来说,初读时差异可能并不明显。