sql查詢練習題

來源:互聯網
上載者:User

標籤:des   os   io   art   ar   工作   sql   res   

--(1)   查詢20號部門的所有員工資訊。

select * from emp e where e.deptno=20;

--(2)   查詢獎金(COMM)高於工資(SAL)的員工資訊。

select * from emp where comm>sal;

--(3)   查詢獎金高於工資的20%的員工資訊。

select * from emp where comm>sal*0.2;

--(4)   查詢10號部門中工種為MANAGER和20號部門中工種為CLERK的員工的資訊。

select * from emp e

where (e.deptno=10 and e.job=‘MANAGER‘)

or (e.deptno=20 and e.job=‘CLERK‘)

--(5)   查詢所有工種不是MANAGER和CLERK,

--且工資大於或等於2000的員工的詳細資料。

select * from emp

where job not in(‘MANAGER‘,‘CLERK‘) and sal>=2000;

--(6)   查詢有獎金的員工的不同工種。

select * from emp where comm is not null;

--(7)   查詢所有員工工資和獎金的和。

select (e.sal+nvl(e.comm,0)) from emp e;

--(8)   查詢沒有獎金或獎金低於100的員工資訊。

select * from emp where comm is null or comm<100;

--(9)   查詢員工工齡大於或等於10年的員工資訊。

select * from emp where (sysdate-hiredate)/365>=10;

--(10) 查詢員工資訊,要求以首字母大寫的方式顯示所有員工的姓名。

select initcap(ename) from emp;

select upper(substr(ename,1,1))||lower(substr(ename,2)) from emp;

--(11) 顯示所有員工的姓名、入職的年份和月份,按入職日期所在的月份排序,

--若月份相同則按入職的年份排序。

select ename,to_char(hiredate,‘yyyy‘) year,to_char(hiredate,‘MM‘) month

from emp

order by month,year;

--(12) 查詢在2月份入職的所有員工資訊。

select * from emp where to_char(hiredate,‘MM‘)=‘02‘

--(13) 查詢所有員工入職以來的工作期限,用“**年**月**日”的形式表示。

select e.ename,floor((sysdate-e.hiredate)/365)||‘年‘

||floor(mod((sysdate-e.hiredate),365)/30)||‘月‘

||floor(mod(mod((sysdate-e.hiredate),365),30))||‘日‘

from emp e;

--(14) 查詢從事同一種工作但不屬於同一部門的員工資訊。

select a.ename,a.job,a.deptno,b.ename,b.job,b.deptno

 from emp a,emp b

 where a.job=b.job and a.deptno<>b.deptno;

--(15) 查詢各個部門的詳細資料以及部門人數、部門平均工資。

select d.deptno,count(e.empno),avg(e.sal),d.dname,d.loc

from emp e ,dept d

where e.deptno=d.deptno

group by d.deptno,d.dname,d.loc

--(16) 查詢10號部門員工以及領導的資訊。

select * from emp where empno in(

select mgr from emp where deptno=10) or deptno=10;

--(17) 查詢工資為某個部門平均工資的員工資訊。

select * from emp

where sal in(select avg(sal) from emp group by deptno);

--(18) 查詢工資高於本部門平均工資的員工的資訊。

select * from emp e1

where sal >(select avg(sal) from emp e2 where e2.deptno=e1.deptno);

--(19) 查詢工資高於本部門平均工資的員工的資訊及其部門的平均工資。

select e.*,a.avgsal

from emp e,

(select deptno,avg(sal) as avgsal from emp group by deptno) a

where a.deptno=e.deptno and e.sal>a.avgsal;

--(20) 統計各個工種的人數與平均工資。

select count(*),e.job,avg(e.sal) from emp e

group by e.job

--(21) 統計每個部門中各個工種的人數與平均工資。

select deptno,job,count(empno),avg(sal) from emp e

group by e.deptno,e.job

--(22) 查詢所有員工工資都大於1000的部門的資訊。

select * from dept where deptno in

(select deptno from emp

where deptno not in

(select distinct deptno from emp where sal<1000));

--(23) 查詢所有員工工資都大於1000的部門的資訊及其員工資訊。

select * from emp e join dept d

on d.deptno

in (select deptno from emp

where deptno not in

(select distinct deptno from emp where sal<1000))

and d.deptno=e.deptno;

--(24) 查詢所有員工工資都在900~3000之間的部門的資訊。

select * from dept

where deptno not in(

select deptno from emp

where sal not between 900 and 3000);

--(25) 查詢所有工資都在900~3000之間的員工所在部門的員工資訊。

select * from emp a

where a.deptno in

(select distinct e.deptno from emp e

where e.sal between 900 and 3000);

--(26) 查詢每個員工的領導所在部門的資訊。

select d.* from dept d

where d.deptno in

(select distinct e2.deptno from emp e1,emp e2

where e1.empno=e2.mgr);

--(27) 查詢人數最多的部門資訊。

select * from dept

where deptno in

(select deptno from (select count(*) count,deptno from emp group by deptno)

where count in(select max(count)

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

--(28) 查詢30號部門中工資排序前3名的員工資訊。

select * from

(select sal from emp where deptno=30 order by sal desc) e

where rownum<4

--(29) 查詢‘JONES‘員工及所有其直接、間接下屬員工的資訊。

select e.* from emp e

start with ename=‘JONES‘

connect by prior empno=mgr;

---(30)       查詢SCOTT員工及其直接、間接上級員工的資訊。

select e.* from emp e

start with ename=‘SCOTT‘

connect by prior mgr=empno;

--(31) 以樹狀結構查詢所有員工與領導之間的層次關係。

select substr(sys_connect_by_path(ename,‘->‘),3),level

from emp

start with mgr is null

connect by prior empno=mgr;

--(32)向emp表中插入一條記錄,員工號為1357,員工名字為oracle,

--工資為2050元,部門號為20,入職日期為2002年5月10日。

--(33)將各部門員工的工資修改為該員工所在部門平均工資加1000。

update emp e set sal=

1000+(select avg(sal) from emp where deptno=e.deptno);

--(34)查詢工作等級為2級,1985年以後入職的工作地點為DALLAS的員工編號、

--姓名和工資。

select e.ename,e.empno,e.sal from emp e,salgrade s,dept d

where (e.sal between s.losal and s.hisal)

and (s.grade=2)

and to_char(e.hiredate,‘yyyy‘)>1985

and e.deptno=d.deptno

and d.loc=‘DALLAS‘;

--35.部門平均薪水最高的部門編號

   select * from(

   select avg(sal) avgsal,deptno

   from emp group by deptno order by avgsal desc)

   where rownum=1;

select deptno,avg(sal) from emp group by deptno having avg(sal)=(  

   select max(avg(sal)) avgsal

   from emp group by deptno)

--36,部門平均薪水最高的部門名稱

    select d.* from dept d where deptno  in(   

    select deptno from emp group by deptno having avg(sal)=(  

   select max(avg(sal)) avgsal

   from emp group by deptno))

--37.平均薪水最低的部門的部門名稱

    select d.* from dept d where deptno  in(   

    select deptno from emp group by deptno having avg(sal)=(  

   select  min(avg(sal)) avgsal

   from emp group by deptno))

--38.平均薪水等級最低的部門的部門名稱

select d.dname from dept d

where d.deptno in

(select a.deptno from

(select e.deptno from emp e,salgrade s

where (e.sal between s.losal and s.hisal)

group by e.deptno order by avg(s.grade)) a

where rownum=1);

--39.部門經理人中,薪水最低的部門名稱

select dname from dept where deptno=

(select deptno from

(select deptno from emp where job=‘MANAGER‘ group by deptno

order by min(sal)) where rownum=1)

--40.比普通員工的最高薪水還要高的經理人名稱

select ename from emp where sal>

(select max(sal) from emp where job not in

(‘MANAGER‘,‘PRESIDENT‘)) and job=‘MANAGER‘ or job=‘PRESIDENT‘

--41.重複資料刪除部門,但是留下一項

insert into dept values(70,‘RESEARCH‘,‘DALLAS‘)

select deptno,dname,rowid from dept

delete from dept d

where rowid<>

(select min(rowid) from dept where dname=d.dname and d.loc=loc)

--42.更新員工工資為他的主管的工資,獎金

update emp e set sal=(select sal from emp where empno=e.mgr),

comm=(select comm from emp where empno=e.mgr)

update emp e set (sal,comm)=(select sal,comm from emp where empno=e.mgr)

rollback;

select * from emp;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.