Oracle-子查询,分页
1?子查询1.1?子查询sql中查询是可以嵌套的。一个查询可以作为其它一个查询的前提、表。 SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table); 领略子查询的要害在于把子查询看成一张表来对待。外层的语句可以把内嵌的子查询返回的功效当成一张表行使。子查询可以作为一个虚表被行使。 子查询要用括号括起来 将子查询放在较量运算符的右边(加强可读性) 子查询按照其返回功效可以分为单行子查询和多行子查询。 1.1.1?单行子查询当子查询有单行时,可以取单行中的一个字段形成单个值用于前提较量。 ? -- 查询雇员其薪资在雇员均匀薪资以上 -- [1] 查询员工的均匀薪资 select avg(e.sal) "AVGSAL" from emp e --[2] 查询满意前提的雇员 select * from emp e where e.sal > (select avg(e.sal) "AVGSAL" from emp e) 1.1.2?多行子查询? -- 查在雇员中有哪些人是打点者 --【1】查询打点者 select distinct e.mgr from emp e where e.mgr is not null --【2】查询指定列表的信息 in select e.* from emp e where e.empno in (select distinct e.mgr from emp e where e.mgr is not null) 多行子查询返回的功效可以作为 表?行使,凡是团结in、some/any、all、exists。 1.1.3?From后的子查询子查询可以作为一张续表用于from后。 ? -- 每个部分均匀薪水的品级 --【1】部分的均匀薪资 select e.deptno,avg(e.sal) "AVGSAL" from emp e group by e.deptno --【2】求品级 select vt0.deptno,vt0.avgsal,sg.grade from (select e.deptno,avg(e.sal) "AVGSAL" from emp e group by e.deptno) VT0,salgrade sg where vt0.avgsal between sg.losal and sg.hisal ? 例: -- 99 join on select vt0.deptno,avg(e.sal) "AVGSAL" from emp e group by e.deptno) VT0 join salgrade sg on vt0.avgsal between sg.losal and sg.hisal 1.2?TOP-N(A)把select获得的数据集提取前n条数。 rownum:暗示对查询的数据集记录的编号,从1开始。 ? -- 查询前10名雇员 select e.*,rownum from emp e where rownum <= 10 rownum和order-by -- 查询凭证薪资降序,前10名雇员 select e.*,rownum from emp e where rownum <= 10 order by e.sal desc ? |