1. 程式人生 > >《Oracle總結 04》--較為複雜的業務查詢

《Oracle總結 04》--較為複雜的業務查詢

1:檢視與CLARK相同職位的員工
select ename,job from emp_xiangyoulu where job=(
  select job from emp_xiangyoulu where ename='CLARK');

2:檢視低於公司平均工資的員工
select ename,sal from emp_xiangyoulu where sal<(
  select avg(sal) from emp_xiangyoulu);
  
3:檢視與ALLEN同部門的員工
select ename,deptno from emp_xiangyoulu where deptno=(
  select deptno from emp_xiangyoulu where ename='ALLEN');

4:檢視平均工資低於20號部門平均工資的部門平均工資
select avg(sal),deptno from emp_xiangyoulu group by deptno having avg(sal)<(
  select avg(sal) from emp_xiangyoulu where deptno=20);
select deptno,avg(sal) from emp_xiangyoulu group by deptno having avg(sal)<(
  select avg(sal) from emp_xiangyoulu group by deptno having deptno=20);
  
5:檢視低於自己所在部門平均工資的員工
select e.ename,e.sal from emp_xiangyoulu e join
  (select avg(sal) avg_sal,deptno from emp_xiangyoulu group by deptno) m
on e.deptno=m.deptno where e.sal<m.avg_sal;

6:檢視公司工資排名的第2-5名
select * from emp_xiangyoulu order by sal desc;
select * from (
  select rownum rn,e.* from(
     select ename,sal,deptno from emp_xiangyoulu order by sal desc) e)
where rn between 2 and 5;
select * from(
  select ename,sal,deptno from emp_xiangyoulu order by sal desc) 
where rownum<5;

select * from(
  select rownum rn,ename,sal from emp_xiangyoulu order by sal desc) 
where rn between 2 and 5;-->有問題,偽列的序號不是按照sal降序排列,而是提前生成好的;
SELECT ROWNUM rn,e.* FROM (
  SELECT * FROM  emp_xiangyoulu ORDER BY sal desc) e
WHERE rn BETWEEN 2 AND 5;--rn 識別符號無效
select rownum,e.* from(
  select * from emp_xiangyoulu order by sal desc)e 
where rownum between 2 and 5;--無結果
select * from(
  select rownum rn,e.* from(
    select * from emp_xiangyoulu order by sal desc)e)
where rn between 2 and 5;
--檢視公司工資排名的第6-10名
select * from(
  select rownum rn,e.* from(
   select * from emp_xiangyoulu order by sal desc)e)
where rn between 6 and 10;
--檢視公司各部門薪資排名前兩名的員工資訊?
select * from(
  select ename,sal,deptno,row_number() over (partition by deptno order by sal desc)rn
  from emp_xiangyoulu)e
where e.rn<3;

7:檢視CLERK職位的人數和其他職位的總人數各多少?
select count(job) from emp_xiangyoulu where job='CLERK';
select count(1),decode(
  job,'CLERK','職員','其他職位')de from emp_xiangyoulu
group by decode(
  job,'CLERK','職員','其他職位');
--統計10號,20部門以及其餘別的的部門的員工人數;
select count(1),decode(
  deptno,10,'10號部門',20,'20號部門','別的部門')de from emp_xiangyoulu
group by decode(
  deptno,10,'10號部門',20,'20號部門','別的部門');
  
8:檢視每個職位的平均工資排名
select job,avg(sal) a_sal from emp_xiangyoulu 
group by job order by a_sal desc;
8.2:檢視每個職位的員工工資排名
select ename,sal,job,row_number() over(partition by job order by sal desc)rn
from emp_xiangyoulu;

9:檢視每個職位的工資排名,若工資一致,排名一致
select ename,sal,job,rank() over(partition by job order by sal desc)rn from emp_xiangyoulu;

10:檢視每個職位的工資排名,若工資一致,排名一致,不跳名次。
select ename,sal,job,dense_rank() over (partition by job order by sal desc)rn
from emp_xiangyoulu;

11:分別檢視:同部門同職位,同職位,以及所有員工的工資總和
select deptno,job,sum(sal) from emp_xiangyoulu 
group by deptno,job;--結果不含工資總和;
select deptno,job,sum(sal) from emp_xiangyoulu 
group by rollup(deptno,job);

12:分別檢視:同部門同職位,同職位,同部門以及所有員工的工資總和
select deptno,job,sum(sal) from emp_xiangyoulu 
group by cube(deptno,job);

13:分別檢視同部門同職位和同職位的員工的工資總和
14:檢視公司最高工資的員工的名字以及所在部門名稱
select e.ename,d.dname from emp_xiangyoulu e 
  left outer join dept_xiangyoulu d on e.deptno=d.deptno 
where e.sal=(select max(sal) from emp_xiangyoulu);
15:檢視每個部門的最高工資的員工名字
select e.ename,e.sal,e.deptno from emp_xiangyoulu e join(
  select max(sal) max_sal,deptno from emp_xiangyoulu group by deptno)d
on e.deptno=d.deptno and e.sal=d.max_sal;
16:檢視有下屬的員工資訊
select distinct m.* from emp_xiangyoulu e join emp_xiangyoulu m on e.mgr=m.empno;
select * from emp_xiangyoulu where empno in(
 select m.empno from emp_xiangyoulu e join emp_xiangyoulu m on e.mgr=m.empno);