开发者

How to select data with a certain minimum total?

I have one table bbc(country, region, area, population, gdp).

I want to select the regions with a total population of at least 100 million. How can I do it with SQL开发者_开发百科?


SELECT
    country,
    region,
    SUM(population)
FROM
    bbc
GROUP BY
    country,
    region
HAVING
    SUM(population) >= 100000000


select region, sum(population) as population from bbc 
         group by region 
         having sum(population) >= 100000000


I'd vote for Salil's answer, but don't have the reputation just yet . :)

His query returns the regions where the sum of countries populations total more or equal than 100 million.

The first answer is grouped by country and region, so it returns the countries and regions where the population in a country is greater or equal than 100 mil.

So the first answer misses the case when all countries in a region have < 100 mil population each, but >= 100 mil together.


SELECT region FROM bbc GROUP BY region HAVING `SUM(population) >= 100000000`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜