白日梦想jun 2016-05-07 13:41 采纳率: 100%
浏览 2185
已采纳

SQL 相关子查询与group by的问题,谢谢指导

student(sno,sname,sdept)

course(cno,cname,ccredit)

sc(sno,cno,grade)

1、查询每个系的最高成绩

select x.sno,sdept,grade from student x,sc
where x.sno = sc.sno
and grade =(select max(grade) from sc,student
where sc.sno=student.sno and sdept=x.sdept)

select x.sno,sdept,grade from student x,sc
where x.sno = sc.sno
and grade in(select max(grade) from sc,student
where sc.sno=student.sno and sdept=x.sdept group by sdept)


select sdept,max(grade) from sc,student where sc.sno=student.sno group by sdept

//上面这三种写法结果是一样的,所以我想问第二种的写法的group by sdept是不是多余的呢?

//另外,如果输出要加学号,为什么直接这样是输出了所有,又错了呢?
    select sdept,sc.sno,max(grade) from sc,student where sc.sno=student.sno group by sdept,sc.sno
  • 写回答

1条回答

  • danielinbiti 2016-05-07 13:53
    关注
     select sdept,sc.sno,max(grade) from sc,student where sc.sno=student.sno group by sdept,sc.sno
     这里group by后面有两个字段sdept和sno,表示按系,按学生分组,也就是每个系中每个学生的课程中的最高分,不是每个系所有学生的最高分
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式