這個(gè)板塊練習(xí)比較少,主要是GROUP BY和 HAVING 的用法。
表結(jié)構(gòu)如下:

For each continent show the continent and number of countries.
SELECT continent,COUNT(name)
FROM world
GROUP BY continent
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 continent the result is that you get only one row for each different value of continent. 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.
List the continents that have a total population of at least 100 million.
SELECT continent FROM world
GROUP BY continent
HAVING SUM(population)>=100000000
注意這里的having語句,而不是用where語句,having 是在分組之后整理結(jié)果的,而where是在分組之前。
練習(xí)網(wǎng)址:https://zh.sqlzoo.net/wiki/SUM_and_COUNT