SQLZOO刷题记录5——SUM and COUNT
World Country Profile: Aggregate functions(世界國家概況:綜合功能)
題目7:Counting big countries in each continent
?For each continent show the continent and number of countries with populations of at least 10 million.
(對于每個大洲,顯示大洲和人口數量超過10000萬的國家數量)
解題7:
SELECT continent, COUNT(name) FROM world WHERE population >= 10000000 GROUP BY continent題目8:Counting big continents
List the continents that have a total population of at least 100 million.
(列出大洲,大洲的總人口數至少是100000萬)
解題8:
SELECT continent FROM world GROUP BY continent HAVING SUM(population) >= 100000000注意:對比題目7和題目8,WHERE,GROUP BY 與 GROUP BY , HAVING 的關系。前者在分組之前進行篩選,后者在分組后進行篩選。
測試 SUM and COUNT Quiz
3. Select the list of core SQL aggregate functions(sql中的聚集函數)
AVG(), COUNT(), MAX(), MIN(), SUM()
?
4. Select the result that would be obtained from the following code:
SELECT region, SUM(area)FROM bbc WHERE SUM(area) > 15000000 GROUP BY region5. Select the statement that shows the average population of 'Poland', 'Germany' and 'Denmark'
?解題5:SELECT AVG(population) FROM bbc WHERE name IN ('Poland', 'Germany', 'Denmark')
注意:SQL 中等于 后面是某一個特定值,而IN 后面可以是一個結果集。如果結果集只有一個值時“=”等價于“IN” ,如果結果集有多個值時,IN 等價于 多個 “=”或。
col = 'A' 等價于 col IN ('A')
col = 'A' or col ='B' or col ='C' 等價于 IN('A','B','C')
6. Select the statement that shows the medium population density of each region
查找每個區域的人口密度,注意此處的人口密度是針對區域來說,也就是區域內的指標總和。
SELECT region, SUM(population)/SUM(area) AS density FROM bbc GROUP BY region?
7.Select the statement that shows the name and population density of the country with the largest population
查找人口數量最多的國家和人口密度
SELECT name, population/area AS density FROM bbc WHERE population = (SELECT MAX(population) FROM bbc)The nobel table can be used to practice more SUM and COUNT functions.
題目9:Show the years in which three prizes were given for Physics.
nobel(yr, subject, winner)解題9:
SELECT yr FROM nobel WHERE subject = 'Physics' GROUP BY yr HAVING COUNT(winner) = 3注意:根據題意的理解,確定WHERE ,GROUP BY 和 HAVING 中各自對應什么條件。
題目12:GROUP BY yr, subject
Show the year and subject where 3 prizes were given. Show only years 2000 onwards.
顯示頒發3個獎項的年份和主題。只顯示2000年及以后。
nobel(yr, subject, winner)解題12:
SELECT yr, subject FROM nobel WHERE yr >= 2000 GROUP BY yr, subject HAVING COUNT(subject) = 3總結
以上是生活随笔為你收集整理的SQLZOO刷题记录5——SUM and COUNT的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页验证
- 下一篇: 黄金车厘子文案:黄金车厘子发朋友圈文案,