# SQL学习小坑之ALL GROUP BY MAX()

9.找出洲份，當中全部國家都有少於或等於 25000000 人口. 在這些洲份中，列出國家名字name，continent 洲份和population人口。

``````SELECT continent,name,population FROM world x
WHERE  ALL(SELECT population FROM world y WHERE  x.continent =y.continent ) <=25000000
``````

operand comparison_operator ALL (subquery)

``````SELECT continent,name,population FROM world x
WHERE 25000000  >= ALL(SELECT population FROM world y WHERE  x.continent =y.continent )
``````

so反过来就ok了，我好啰嗦啊，如上。

``````SELECT continent,name,population FROM world
WHERE  25000000 >=ALL(SELECT MAX(population) FROM world  GROUP BY continent )
``````

``````SELECT name,continent,area FROM world x
WHERE 25000000 >=(SELECT max(population) FROM world y WHERE x.continent =y.continent)
``````

``````SELECT name,continent,area FROM world x
WHERE 25000000>=ALL(SELECT population FROM world y WHERE x.continent = y.continent )
``````

``````SELECT name,continent,area FROM world
WHERE continent IN (
SELECT continent FROM world GROUP BY continent HAVING MAX(population) <= 25000000)
``````

PS:sql语句中where与having的区别

``````SELECT name,continent,area FROM world
WHERE continent NOT IN(SELECT DISTINCT(continent) FROM world WHERE population >25000000)
``````

Done！

原文作者：织田茄子
原文地址: https://www.jianshu.com/p/023d8e1dfdfb
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。