views:

112

answers:

4

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?

+10  A: 
SELECT
    country,
    region,
    SUM(population)
FROM
    bbc
GROUP BY
    country,
    region
HAVING
    SUM(population) >= 100000000
Robin Day
+1 for grouping by `country, region`. Good thinking ahead there.
Matt
+3  A: 
select region, sum(population) as population from bbc 
         group by region 
         having sum(population) >= 100000000
Salil
A: 

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.

ceteras
A: 

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

Dharmendra