Thursday, February 17, 2011

ORDER BY AND ROWNUM ISSUE IN ORACLE

Example :

WRONG ONE :
1. select * from table where rownum <= 2 order by 1 desc
2.select xam from ho_aa where ROWNUM = 1 ORDER BY LOG_TIME DESC
CORRECT ONE :
1. select * from (select * from table order by 1 desc) where rownum <= 2
2.select xam from (select xam from ho_aa ORDER BY LOG_TIME DESC) where ROWNUM = 1

REASON : Rownum is assigned after the row is selected BUT BEFORE ORDER BY

No comments:

Post a Comment