SQL ZOO 按字母顺序列出每个大陆和国家名称

SQL ZOO List each continent and the name of the country that comes first alphabetically

我很困惑为什么不能

Select x.continent, x.name
From world x
Where x.name <= ALL (select y.name from world y where x.name=y.name)
ORDER BY name

任何人都可以向我解释为什么它必须是 x.continent=y.continent 而不是 x.name=y.name 吗?

Table

当您使用 x.name=y.name 时,如果两个实例具有相同的国家/地区名称,则您正在比较来自 x 的国家/地区名称和来自 y 的国家/地区名称。那基本上就是 return 你完整的 table x。

您想使用 x.continent=y.continent 是因为您只想比较来自 x 的实例的国家名称与来自 y 的实例的国家名称(如果它们共享同一大陆)。

让我用一个例子一步一步地说明这一点: 这里我们有一个 table 世界,我填充了一些数据:

world: 

Select  x.continent, x.name
From world x
ORDER BY  name

continent       name
Asia            Afghanistan
Europe          Albania
Africa          Algeria  
Europe          Andorra
Africa          Angola
SouthAmerica    Bolivia
SouthAmerica    Brazil
Europe          Hungary
Asia            Japan
Africa          Nigeria
SouthAmerica    Peru
Asia            Taiwan

当您在子查询中没有 WHERE 子句的情况下执行此查询时:

Select  x.continent, x.name
From world x
Where x.name <= ALL (select y.name from world y)
ORDER BY name

你明白了

continent   name
Asia        Afghanistan

这是因为 where 子句过滤掉了除一个国家以外的所有国家

where x.name <= (Afghanistan,Taiwan,Japan,
                 Albania,Hungary,Algeria,Nigeria,Andorra,
                 Angola,Bolivia,Peru,Brazil)

也就是按字母顺序排在第一位的国名是阿富汗。

但是因为我们想要获得每个大陆中的第一个国家,我们将添加x.continent=y.continent到我们的子查询

Select  x.continent, x.name
From world x
Where x.name <= ALL (select y.name from world y where x.continent=y.continent)
ORDER BY name

下面发生的事情是,现在我们只比较来自 x 的实例的国家名称和来自 y 的实例的国家名称,如果它们共享同一大陆。所以以亚洲大陆为例:

Japen 被过滤掉是因为 Japan <= All(Afghanistan,Taiwan,Japan) 是错误的,因为日本不小于或等于阿富汗(A 在 J 之前)

台湾被过滤掉是因为 Taiwan <= All(Afghanistan,Taiwan,Japan) 是错误的,因为台湾不小于或等于阿富汗。

阿富汗没有被过滤掉,因为 Afghanistan <= All(Afghanistan,Taiwan,Japan) 为真,因为阿富汗等于阿富汗

但是,如果您在子查询中使用 x.name=y.name,那么您实际上是在将每个国家与其自身进行比较,并且它们都将包含在您的最终结果集中,因为所有国家名称都等于其自身的国家名称。

希望对您有所帮助,欢迎来到 Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。”

 select A.continent, A.name 
 from
 (Select  x.continent, x.name, RANK() OVER(PARTITION BY x.continent ORDER BY x.name 
 asc)AS Rank  
 From world x
 group by  x.continent, x.name)A
 where
 Rank = 1

这是正确答案:

Select continent,name 
from world x
Where x.name <= ALL(select y.name from world y
                    where x.continent=y.continent)
ORDER BY continent

这个很简单,因为 min func 将 return 按字母顺序排列的第一个单词。

SELECT continent, name FROM world x WHERE name = (SELECT min(name) FROM world y WHERE x.continent = y.continent );

这也产生了正确的答案。

SELECT continent, name
FROM world a
WHERE name <= ALL(SELECT name FROM world b
               WHERE a.continent = b.continent)

我试过了,它也有效:

SELECT continent, MIN(name)
FROM world
GROUP BY continent

这里要注意一点——ORDER BY 对于这个问题的正确答案是不必要的,最简单的解决方案是:

SELECT continent, name
FROM world w1
WHERE w1.name <= ALL(SELECT name 
                       FROM world w2
                       WHERE w1.continent=w2.continent)

按字母顺序列出每个大洲和国家/地区的名称。

Select continent, name from world x where name in (select min(name) from world y where x.continent= y.continent)

SELECT continent, name 
FROM world as x
WHERE name = ALL(SELECT min(name)
                 FROM world y
                 WHERE x.continent = y.continent);
SELECT
    continent, name
FROM
    world x
WHERE
    name <= ALL(SELECT name FROM world y WHERE x.continent=y.continent ORDER BY name )
ORDER BY continent

return 有序国家名称列表,然后 return 此列表的第一个条目,最后按大陆对外部 select 查询进行排序。

另一种使用 rank() 函数的方法也有效

select continent, name
from
(
    select continent, name, rank() over (partition by continent order by continent, name) as rn
    from world
    group by continent, name
    order by rn
) as a 
where rn = 1
order by continent

您可以通过这种方式查询

    SELECT MIN(continent), MIN(name) 
    FROM world
    GROUP By continent
with new as (
    select continent, name , row_number() over (partition by continent order by name) as row 
    from world
)

select continent,name
from new
where row = 1

适用于上述问题的最简单查询:

Select 洲,最小(名称) 来自世界 按大陆分组 ;

select continent, name
from world x
where name = 
(select name from world y where x.continent= y.continent order by name limit 1)

只需对相关子查询中的元素进行排序,其中第一个元素使用限制。