SQL ZOO 練習(xí)題解法【SUM and COUNT】

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


world.png

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容