日期:2014-05-16  浏览次数:20488 次

关于子查询中不能使用order by 的理解
select * from (select * from emp e where e.sal < 1000 order by e.empno) t order by t.sal desc;

这条SQL的子查询里面  包含了子查询 就可以执行
而下面这条SQL
引用
select * from emp where dept in
   (select dept from valid_deps
    where dept_head='sally'
   order by dept);

在执行的时候则会报

具体原因:
在8i以前,子查询中不能包含Order By子句。

从8i开始,子查询可以有Order By,但必须是有top-n的这种子查询时才能用。

所谓的top-N查询就是:
TOP-N一般是指最大的n条记录或着是最小的n条记录。
如:
select rownum , t.col1, t.col2, ... from
( select col1, col2, ... from tab
  order by col1 ) t
where rownum <= n;

参考文章:http://blog.csdn.net/lee576/article/details/2685647