代码改变世界

Oracle 练习

2017-11-09 09:39  你若安好!  阅读(447)  评论(0编辑  收藏  举报

练习

1) 选择在部门 30 中员工的所有信息

SQL>select empno,ename,job,mgr,hiredate,sal,comm,deptno from  emp where deptno=30;

2) 列出职位为(MANAGER)的员工的编号,姓名

SQL>select empno,ename from emp where job='MANAGER';

3) 找出奖金高于工资的员工

SQL> select ename from emp where comm>sal;

4) 找出每个员工奖金和工资的总和

SQL> select ename,'总和',nvl(comm,0)+sal from emp;

5) 找出部门 10 中的经理(MANAGER)和部门 20 中的普通员工(CLERK)

SQL> select ename,deptno,job,sal from emp where deptno=10 and job='MANAGER';

SQL>select ename,deptno,job,sal from emp where deptno=20 and  job='CLERK';

6) 找出部门 10 中既不是经理也不是普通员工,而且工资大于等于 2000 的员工

SQL>select ename from emp where deptno=10 and job<>'MANAGER'and job<>'CLERK'and sal>=2000; 

7) 找出有奖金的员工的不同工作

SQL> select job from emp where job in(select ename from emp where   comm>=0);

8) 找出没有奖金或者奖金低于 500 的员工

SQL> select ename from emp where (comm is null)or(comm<500);

9) 显示雇员姓名,根据其服务年限,将最老的雇员排在最前面

SQL> select ename,hiredate from emp order by hiredate;

10) 分组统计各部门下工资>500 的员工的平均工资、;

SQL> select deptno,avg(sal) from emp where sal>500

     group by deptno;

11) 统计各部门下平均工资大于 500 的部门

SQL>select deptno,avg(sal) from emp

group by deptno

having avg(sal)>500;

12) 算出部门 30 中得到最多奖金的员工奖金

SQL> select deptno,max(nvl(comm,0)) maxcomm from emp group by  deptno having deptno=30;

13) 算出部门 30 中得到最多奖金的员工姓名

SQL> select deptno,ename from emp where comm=(select max(nvl(comm,0)) maxcomm from emp where deptno=30);

14) 算出每个职位的员工数和最低工资

SQL> select job,count(*),min(sal) nums from emp group by job;

15) 列出员工表中每个部门的员工数和部门 

SQL> select deptno,count(*) nums from emp group by deptno;

16) 得到工资大于自己部门平均工资的员工信息

SQL> select a.* from emp a,(select deptno,avg(sal) sal from emp group by deptno)b where a.deptno=b.deptno and a.sal>b.sal;

*--大于所有平均工资的员工信息:SQL> select * from emp where sal>(select avg(sal) sal from emp);

17) 分组统计每个部门下,每种职位的平均奖金(也要算没奖金的人)和总工资(包括奖金)

SQL> select deptno,job,avg(sal+nvl(comm,0)),sum(sal) from emp  group by deptno,job;