表格:name          continent        population
China          Asia            1300000000
...             ...              ...问题:
Find the largest country in each continent, show the continent, the name and the population:
答案:
SELECT continent, name, population FROM world x
  WHERE population >= ALL
    (SELECT population FROM world y
        WHERE y.continent=x.continent
          AND population>0)疑问:在这里设x和y表是什么意思?为什么最后还要加上population>0的条件?谢谢!

解决方案 »

  1.   

    x和y是别名,如果不加别名,由于你都是用同一个表,所以会导致SQLServer无法识别字段最终是来源于哪个表。这在自连接的时候常用。
    我个人觉得这个population>0好像没多大必要,一般用处只是减少计算population=0的数据,减少计算范围,加快速度而已。
      

  2.   


    谢谢!我去复习一下自连接另外population>0我试过如果去掉的话,结果会少一行,不清楚是为什么原题来自这里的第六题:http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial非常感谢!
      

  3.   


    All:对所有数据都满足条件,整个条件才成立,例如:5大于所有返回的id
    select *
    from #A
    where 5>All(select id from #A)数据中population应该有<=0的