查询每个系年龄最大的学生的学号,年龄

wxxloveu 2007-12-14 11:20:41
查询每个系年龄最大的学生的学号,年龄

select sno,max(sage) max
from student
group by sdept

服务器: 消息 8120,级别 16,状态 1,行 1
列 'student.sno' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
...全文
3099 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyfearth 2008-11-02
  • 打赏
  • 举报
回复 2
1楼正解
2楼求的是所有学生中年龄最大的
3楼没有考虑到有两个学生sage相同,但在不同系,
而且一个在他的系中最大,而另一个不是,
你这样写,两个都会打印出来,显然,第二个人不符合要求~!
wxxloveu 2007-12-14
  • 打赏
  • 举报
回复
select sno,max(sage) maxage
from student
where sage in (select max(sage)
from student
group by sdept
)
group by sno
homel 2007-12-14
  • 打赏
  • 举报
回复
select sno,cname,age
from student
where age =(select max(age) from student)
dawugui 2007-12-14
  • 打赏
  • 举报
回复 2
select a.* from student a where sage = (select max(sage) from student where sdept = a.sdept)

34,835

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧