sqlzoo.net刷题4

SELECT name, continent FROM world a
WHERE population > 
        (SELECT MAX(population) * 3 
         FROM world b
         WHERE a.continent = b.continent 
           AND a.name <> b.name)

http://dba.stackexchange.com/questions/4066/cant-retrieve-data-of-countries-and-regions

老外也有在论坛为这题发愁的,幸好下面有人解答,我找着思路把这题给抄了

题目问题是:

Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.

找到那些国家超出每个邻居国家人口3倍以上,按理说这种题,用过程化语言其实相当容易解答的,排序之后直接找到第一大的元素与第二大的元素进行比较就好了,

不过SQL还是复杂并且难以理解。

 

这题的主要在于子查询,这里一定要搞清楚的一个问题是 集聚函数是在where语句完成之后,再进行计算的,也就是说子查询中的MAX函数是在排除

外部查询当前国家之后 进行集聚,找到人口最多的国家并将其×3与外部查询的人口进行比较。

 

posted on 2016-08-28 06:18  winters86  阅读(262)  评论(0编辑  收藏  举报