sql優化小技巧

select * from 
(select id,name,(select sum(score) from SCORES t2 where t2.studentid=t1.id) as totalscore
from STUDENT t1
group by id,name
order by totalscore desc) t3
where t3.rownum <=3

學生表(student):id name class 科目表(subject):id  name
成績表:studentid  , subjectid  ,score   用Oracle 編寫一個sql 查出學生的 id name 和分數(總成績)取前三名學生

select t1.id,t1.name,t3.totalscore
from STUDENT t1,(select t2.studentid,sum(score) as totalscore from score t2 group by studentid order by totalscore desc)t3
where t1.id = t3.studentid
and rownum<=3;

將子查詢放在from語句中只執行一次,比放在select語句中..效率高多了sql

相關文章
相關標籤/搜索