By including a GROUP BY clause functions such as
SUM and COUNT
are applied to groups of items sharing values. When you specify
GROUP BY region
the result is that you get only one row for each different value
of region
. All the other columns must be "aggregated" by one of SUM,
COUNT ...
The HAVING clause allows use to filter the groups which are
displayed. The WHERE clause filters rows before the aggregation,
the HAVING clause filters after the aggregation.
If a ORDER BY clause is included we can refer to columns by
their position.
1. COUNT
For each region show the number of countries:
bbc(name, region, area, population, gdp)
Results
2. SUM
For each region show the total population:
bbc(name, region, area, population, gdp)
Results
3. WHERE and GROUP BY
The WHERE filter takes place before the aggregating function.
For each relevant region show the number of countries that has a population
of at least 200000000.
(You may recall that there are 4 such countries: China, India, USA
and Indonesia)
Results
4. 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 regions with a total population of at
least half a billion.