Oracle数据库 常见的SQL题,复习

01.查询员工表所有数据,并说明使用*的缺点
select * from emp


02.查询职位(JOB)为'PRESIDENT'的员工的工资
select sal from emp where job='PRESIDENT'


 03.查询佣金(COMM)为0或为NULL的员工信息
select * from emp where COMM=0 or comm=null


 04.查询入职日期在1982-5-1到1981-12-31之间的所有员工信息
select * from emp where HIREDATE between to_date('1981-12-31','yyyy-mm-dd') AND to_date('1982-05-01','yyyy-mm-dd')


 select * from emp where to_char (HIREDATE,'yyyy-mm-dd') between '1981-12-31' and '1982-05-01'
 
 05.查询所有名字长度为4的员工的员工编号,姓名
select empno,ename  from emp where ename like '____'
select empno,ename from emp where length(ename)=4;


 06.显示10号部门的所有经理('MANAGER')和20号部门的所有职员('CLERK')的详细信息
select * from emp where (deptno=10  and job= 'MANAGER') or (deptno=20 job='CLERK')


 07.显示姓名中没有'L'字的员工的详细信息或含有'SM'字的员工信息
select * from emp where ename  not like '%L%' or ename like '%SM%'


 08.显示各个部门经理('MANAGER')的工资
select SAL from emp where job='MANAGER'


 09.显示佣金(COMM)收入比工资(SAL)高的员工的详细信息
select * from emp where comm > sal


 10.把hiredate列看做是员工的生日,求本月过生日的员工(考察知识点:单行函数)
 select * from emp where to_char(hiredate,'mm') = to_char(sysdate,'mm')


 11.把hiredate列看做是员工的生日,求下月过生日的员工(考察知识点:单行函数)
 select * from emp where to_char(hiredate,'mm') = to_char(add_months(sysdate,1),'mm')
 
 12.求1982年入职的员工(考察知识点:单行函数)
 select * from emp where  to_char(hiredate,'yyyy') > '1982'
 
 13.求1981年下半年入职的员工(考察知识点:单行函数) 
 select * from emp where  to_char(hiredate,'mm') >=6
 
 14.求1981年各个月入职的员工个数(考察知识点:组函数)
 select to_char(hiredate,'mm')  ,count(*)   from emp where to_char(hiredate,'yyyy')='1981' group by to_char(hiredate,'mm')


 15.查询各个部门的平均工资
 select deptno ,avg(sal) from emp group by deptno


 16.显示各种职位的最低工资
select job ,min(sal) from emp group by job;
 
 17.按照入职日期由新到旧排列员工信息
select * from emp order by hiredate desc
 
 18.查询员工的基本信息,附加其上级的姓名(内连接)
select t1.* ,t2.ename  上级 from emp  t1 join emp t2 on t1.empno= t2.mgr;
 
 19.显示工资比'ALLEN'高的所有员工的姓名和工资

select ename ,sal from emp where sal> (select sal from emp where ename='ALLEN');

 

 select t1.* from emp t1 join emp t2 on nvl(t1.sal,0)> nvl(t2.sal,0) where t2.ename='ALLEN';
 
 20.显示与'SCOTT'从事相同工作的员工的详细信息
select t1.* from emp t1 join emp t2 on t1.job = t2.job where t2.ename='SCOTT'
 
 21.显示销售部('SALES')员工的姓名
select ename from emp e, dept d where e.deptno = d.deptno and d.dname='SALES';
 
 22.显示与30号部门'MARTIN'员工工资相同的员工的姓名和工资
select t1.ename ,t1.sal from emp t1 join emp t2 on t1.sal= t2.sal where t2.ename='MARTIN'
 
 23.查询所有工资高于平均工资的销售人员('SALESMAN')
select * from emp where sal > (select nvl(avg(sal),0) from emp)
 
 24.显示所有职员的姓名及其所在部门的名称和工资
select ename ,dname,sal  from emp e,dept d where e.deptno=e.deptno
 
 25查询在研发部('RESEARCH')工作员工的编号,姓名,工作部门,工作所在地
select empno, ename ,dname , loc from emp e,dept d where e.deptno=e.deptno and dname='RESEARCH'
 
 26.查询各个部门的名称和员工人数
select d.dname,count(*) from emp e,dept d where e.deptno=d.deptno group by d.dname
 
 27.查询 各个职位 员工工资大于平均工资(平均工资包括所有员工)的人数 和员工职位
select count(*) ,job from emp where sal >(select nvl(avg(sal),0) from emp) group by job
 
 28.查询工资相同的员工的工资和姓名 (去重复)

select t1.ename ,t1.sal from emp  t1 join emp t2 on t1.sal=t2.sal where t1.empno!=t2.empno
  
 29.查询工资最高的3名员工信息(先排序再选前三个)
select *  from emp where empno in ( select empno from ( select empno from emp order by sal desc)t
where rownum<=3);
 
 30.按工资进行排名,排名从1开始,工资相同排名相同(如果两人并列第1则没有第2名,从第3名继续排)
select e.*,(select count(*) from emp where sal>e.sal)+1 rank from emp e order by rank;
 
 31.求入职日期相同的(年月日相同)的员工
select t1.* from emp t1 join emp t2 on  to_char(t1.hiredate,'yyyy-mm-dd')= to_char(t2.hiredate,'yyyy-mm-dd') where t1.empno != t2.empno;
 
 32.查询每个部门的最高工资
 select deptno,max(sal) from emp group by deptno order by deptno;


 33.查询每个部门,每种职位的最高工资
select deptno,job,max(sal) from emp group by deptno,job order by deptno,job; 
 
 34.查询每个部门有多少人
select deptno,count(*) from emp group by deptno; 

posted @ 2015-09-26 17:39  一杯半盏  阅读(497)  评论(0编辑  收藏  举报