1. 程式人生 > >《Oracle總結 03》--常見的業務查詢

《Oracle總結 03》--常見的業務查詢

1:檢視工資高於2000的員工
select ename,sal from emp_xiangyoulu where sal>2000;
select ename,deptno,sal from emp_xiangyoulu where sal>2000 order by sal desc;

2:檢視職位不是"CLERK"的員工
select ename,job from emp_xiangyoulu where job <>'CLERK';
select ename,job from emp_xiangyoulu where job != 'CLERK';

3:檢視工資在1000-2500之間的員工 
select ename,sal from emp_xiangyoulu where sal between 1000 and 2500;
select ename,deptno,sal from emp_xiangyoulu where sal between 1000 and 2500 order by sal;

4:檢視名字是以K結尾的員工
select * from emp_xiangyoulu where ename like '%K';
select ename,deptno,sal from emp_xiangyoulu where ename like '%K';

5:檢視20,30號部門的員工
select * from emp_xiangyoulu where deptno=20 or deptno=30;
select * from emp_xiangyoulu where deptno in(20,30);

6:檢視獎金為NULL的員工
select ename,comm from emp_xiangyoulu where comm is null;
select * from emp_xiangyoulu where comm is null order by deptno;

7:檢視年薪高於20000的員工
select ename,sal*12 from emp_xiangyoulu where sal*12>20000;
select ename,sal*12 totalSal from emp_xiangyoulu where sal*12>20000;

8:檢視公司共有多少種職位
select count(distinct job) from emp_xiangyoulu ;
--select count(job) from (select ceil(avg(sal)),job from emp_xiangyoulu group by job);

9:按部門號從小到大排列檢視員工
select * from emp_xiangyoulu order by deptno asc;--順序(預設)
select * from emp_xiangyoulu order by deptno desc;--倒敘

10:檢視每個部門的最高,最低,平均工資,和工資總和
select max(sal),min(sal),ceil(avg(sal)),sum(sal),deptno from emp_xiangyoulu 
group by deptno order by deptno;

11:檢視平均工資高於2000的部門的最低薪水
--select avg(sal),deptno from emp_xiangyoulu group by deptno where avg(sal)>2000; --報錯!
select avg(sal),deptno from emp_xiangyoulu group by deptno having avg(sal)>2000;
select avg(sal),deptno from emp_xiangyoulu group by deptno having deptno in(
  select deptno from emp_xiangyoulu group by deptno having avg(sal)>2000);

12:檢視在NEW YORK工作的員工
select * from emp_xiangyoulu e join dept_xiangyoulu d 
  on e.deptno=d.deptno and d.loc='NEW YORK'; 
select * from emp_xiangyoulu e,dept_xiangyoulu d where e.deptno=d.deptno and d.loc='NEW YORK';

13:檢視所有員工及所在部門資訊,若該員工沒有部門,則部門資訊以NULL顯示
select * from emp_xiangyoulu e left outer join dept_xiangyoulu d on e.deptno=d.deptno;

14:檢視ALLEN的上司是誰
select e.ename,e.mgr,m.empno,m.ename from emp_xiangyoulu e join emp_xiangyoulu m 
on e.mgr=m.empno and e.ename='ALLEN';

15:檢視SMITH的上司在哪個城市工作?
select m.ename,d.loc from emp_xiangyoulu e join emp_xiangyoulu m on e.mgr=m.empno
join dept_xiangyoulu d on e.deptno=d.deptno 
where e.ename='SMITH';

16:檢視平均工資高於2000的那些部門名字以及所在城市?
select distinct e.deptno,d.dname,d.loc from emp_xiangyoulu e join dept_xiangyoulu d on e.deptno=d.deptno 
and e.deptno in(select deptno from emp_xiangyoulu group by deptno having avg(sal)>2000);
SELECT e.deptno,AVG(e.sal),d.dname,d.loc FROM emp_xiangyoulu e ,dept_xiangyoulu d
WHERE e.deptno=d.deptno
GROUP BY e.deptno,d.dname,d.loc  HAVING AVG(e.sal)>2000;

17:在NEW YORK工作的員工有多少人?
select count(1) from emp_xiangyoulu e join dept_xiangyoulu d
on e.deptno=d.deptno and d.loc='NEW YORK';

18:在DALLAS工作的員工的平均工資是多少?
select avg(e.sal) from emp_xiangyoulu e join dept_xiangyoulu d 
on e.deptno=d.deptno where d.loc='DALLAS';