student(sno,sname,,sdept)
course(cno,cname,ccredit)
sc(sno,cno,grade)
為什麼這樣寫不行呢?
//查詢CS系成績最高的學生的學號、姓名以及成績
select student.sno,sname,grade from student,sc where student.sno = sc .sno
and grade=(select max(grade) from sc where sc.sno = sno and sdept = 'cs')
還有一個問題;
//查詢每個系的最高成績
select sdept,max(grade) from sc,student where sc.sno=student.sno group by sdept;這樣寫把每個系的最高分列出來了,當我想把sno列出來,所以加上了sc.sno,下面的就什麼結果也沒有,是錯的。
--select sdept,sc.sno,max(grade) from sc,student where sc.sno=student.sno group by sdept,sc.sno
那這樣的問題的group by到底該怎麼寫呢?如果我想把學號等也列出來是不是只可以寫成相關查詢呢?
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 student.sno,sname,grade from student,sc where student.sno = sc .sno
and grade=(select max(grade) from sc where sc.sno = sno and sdept = 'cs')
這句不對的原因在於,下面子句中,左邊的sdept不存在,在sc表中沒有
(select max(grade) from sc where sc.sno = sno and sdept = 'cs')