SQL max函数获取其他表

SQL max function get other tables

我有一个简单的关系“海”,只有两列,一列称为“名称”,另一列称为“深度”。使用以下命令,我可以输出属性深度中的数字最大值:

SELECT max(depth) FROM sea;

我也在尝试获取最大深度的名称,以便输出:

    name | depth
___________________
pacific  | 11034

也有输出的方法吗?

我已经用 group by 尝试过,还尝试加入 table 本身接收其他属性,但没有找到任何解决方案。

使用order by:

select s.*
from sea s
order by s.depth desc
fetch first 1 row only;

注意:某些数据库不支持 fetch——标准的 SQL 语法——因此您可以使用 limitselect top (1) 或一些类似的结构。

以上returns正好一行,即使有并列。如果你想要 all 行,那么一个简单的方法是子查询:

select s.*
from seas s
where s.depth = (select max(s2.depth) from seas s2);