oracle簡單和複雜的SQL語句練習

      在寫這第二篇文章的時候,我又想着該用什麼幽默風趣的詞來開始我的開頭,好讓你們能夠有興趣看我的第三篇文章。此刻的我並沒有像第一次一樣不知所宗,這次我並沒有忘記主題,我知道我是要寫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');










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章