您的位置:首页 > 数据库 > Oracle

oracle简单和复杂的SQL语句练习

2018-02-09 15:28 441 查看
      在写这第二篇文章的时候,我又想着该用什么幽默风趣的词来开始我的开头,好让你们能够有兴趣看我的第三篇文章。此刻的我并没有像第一次一样不知所宗,这次我并没有忘记主题,我知道我是要写sql的内容的,而且还是那种很长很硬的sql,如果你是个sql爱好者你可能会说,这家伙的sql语句真给力,特别的实用,如果你只是想简单学习下,我怕你会骂我说,这家伙真脑残从哪里找的这么硬的sql。哈哈,话不多说,一起来练习SQL语句吧,这次还是15个例子。
1.查询工资在0-1000,1000-2000,2000-3000,3000以上各个工资范围的员工数。
select sum(case when sal>0 and sal<1000 then 1 else 0 end)  "0<sal<1000"    ,
            sum(case when sal>1000 and sal<2000 then 1 else 0 end) as "1000<sal<2000",
            sum(case when sal>2000 and sal<3000 then 1 else 0 end) as "2000<sal<3000",
            sum(case when sal>=3000  then 1 else 0 end) as "sal>3000"

            from emp;
2. ---要求查询出: 部门名称,部门的员工数,部门的平均工资,部门的最低收入雇员的姓名
select d.dname as "部门名称",
        tt.c as "部门的员工数",
       tt.avgsal as "部门的平均工资", 
       e.ename  as "部门的最低收入雇员的姓名"
  from emp e, dept d,
  (select deptno,min(sal) sals,count(1) c,avg(sal) avgsal from emp group by deptno) tt

 where d.deptno = e.deptno and e.deptno=tt.deptno and tt.sals=e.sal
3.删除学生表中重复的记录
delete from student1 where  sno in (select  sno from student1 group by sno having count(1)>1)

4.--求出每个部门的最低工资的雇员的信息(两种方法)
select * from emp where sal in (select min(sal) from emp  group by deptno)

select * from emp where sal= any(select min(sal) from emp  group by deptno)
5.--查询各个职位员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位(两种方法)
select count(1),job from emp where sal>(select avg(sal) from emp) group by job

select count(1),t1.job from emp t1,(select sal,empno from emp ) t2  where t1.empno=t2.empno and  t2.sal>(select avg(sal) from emp) group by t1.job 

6.--写出一个部门号对应不同员工的记录
select * from emp where deptno in   (select t1.deptno from emp t1 group by t1.deptno having count(1)>2)

7.列出所有员工的姓名及其直接上级的姓名
select e1.ename,e2.ename from emp e1 left join emp e2 on e1.mgr=e2.empno;

8.---返回比本部门平均工资高的员工的empno,ename,deptno,sal,以及平均工资(两种方法)
select empno,ename,deptno,sal,(select avg(sal) from emp t3 where t1.deptno=t3.deptno group by deptno) as avgsal  from emp t1 where sal>(select avg(sal) from emp t2 where t1.deptno=t2.deptno  group by t2.deptno)

select t1.empno,t1.ename,t1.deptno,t1.sal,t2.avgsal from emp t1,(select deptno,avg(sal) avgsal from emp t2 group by t2.deptno) t2 where t1.deptno=t2.deptno and t1.sal>t2.avgsal;

9.---查询与7369或者7499号具有相同job和deptno的其他员工的empno,ename,job和empno
select empno,ename,job,deptno from emp where (job,deptno) in(select job,deptno from emp where empno in(7369,7499))

10.--列出从事同一种工作但属于不同部门的雇员的不同组合(两种方法)
select t1.empno,t1.ename,t1.deptno,t2.empno,t2.ename,t2.job,t2.deptnofrom emp t1, emp t2 where t1.job = t2.job

and t1.deptno <> t2.deptno
select * from emp e1,(select job,deptno from emp) e2 where e1.job=e2.job and e1.deptno<>e2.deptno;

11.查询员工工资2到5名的员工信息
select * from (select rownum r,empno,ename, sal from( select empno,ename,sal from emp order by sal desc  ) ) t

where t.r>=2 and t.r<=5 

12.存在时更新数据
update nx_law_info t1 set (debt_bal, inner_int_cumu,nx_cancel_amt,nx_replace_amt, off_int_cumu) =
(select (nvl(ZHCHBJ,0) + nvl(YUQIBJ,0)) debt_bal,
              (nvl(YINSQX, 0) + nvl(YINSFX, 0) + nvl(YSYJLX, 0) +
               nvl(YSYJFX, 0)) inner_int_cumu,
               nvl(HEXIBJ,0) nx_cancel_amt,
               nvl(ZHHABJ,0) nx_replace_amt,
               (nvl(CUISQX, 0) + nvl(CUISFX, 0) + nvl(CSYJLX, 0) +
               nvl(CSYJFX, 0)) off_int_cumu
          from NX_CBS_ADKZH t2
         where t1.debt_no = t2.dkjeju)
 where exists
 (select t2.dkjeju from NX_CBS_ADKZH t2 where t1.debt_no = t2.dkjeju);13.--列出薪金比smith多的所有员工信息(两种方法)
select * from emp where sal>(select sal from emp where ename='SMITH')

select * from emp e1,(select sal from emp where ename='SMITH') e2 where e1.sal>e2.sal;

14.- 把hiredate列看做是员工的生日,求本月过生日的员工(三种方法)
select * from emp where  extract(month from hiredate)=extract(month from sysdate)
select * from emp where substr(to_char(hiredate,'yyyy-MM-dd'),6,2)=substr(to_char(sysdate,'yyyy-MM-dd'),6,2)

select * from emp where to_char(hiredate,'mm')=to_char(sysdate,'mm');
15.--查询出1981各个月入职的员工数
select count(*), to_char(hiredate,'yyyy-MM')  from emp where to_char(hiredate,'yyyy')='1981'
group by to_char(hiredate,'yyyy-MM') order by to_char(hiredate,'yyyy-MM');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库SQL语句