Oracle課後練習-4

1.顯示姓名倒數第三個字母爲I的僱員的姓名、部門名稱。

select ename,dname 
from emp,dept
where emp.deptno=dept.deptno
And ename like '%I__'

2.在部門號爲10,20,30的部門中查詢出工資超過3000的職工。

select ename,sal,deptno from emp
where sal>3000 and deptno in (10,20,30)

3.查詢工資最高的前3名職工所在的部門名。

select dname from dept
where dept.deptno in (
select deptno from 
(select emp.deptno from emp order by sal desc)
where rownum<=3) 

4.查詢出和Smith同部門、職務也相同的職工。

select * from emp      
where (deptno,job)  in (select deptno,job from emp where ename='SMITH')
And ename<>'SMITH'

5.查詢工資比Smith高的職工人數(分別用子查詢和連接查詢兩種方法完成)

--第一種 子查詢
select count(*) from emp 
where sal>(select sal from emp where ename='SMITH')

--第二種 連接查詢
select count(*) from emp a,
(select * from emp where ename='SMITH') b
where a.sal>b.sal

6.查詢和Smith同一部門,並且崗位爲“CLERK”的職工名(分別用子查詢和連接查詢兩種方法完成)

--第一種 連接查詢
select a.ename from emp a,emp b
where b.ename='SMITH' 
and a.job='CLERK'
and a.deptno=b.deptno

--第二種 子查詢
select ename from emp 
where deptno=(select deptno from emp where ename='SMITH')
and job='CLERK'

7.查詢部門平均工資超過Smith所在部門的平均工資的所有部門號(用子查詢完成)

select deptno,avg(sal) from emp
group by deptno 
having avg(sal)>
(select avg(sal) from emp 
where deptno=(select deptno from emp where ename='SMITH'))

8.顯示所有比其上級管理員更早進入公司的僱員的姓名(Employee)、受僱日期(Emp Hiredate)以及其上級管理員的姓名(Manager)及受僱日期(Mgr Hiredate)。(分別用子查詢和連接查詢完成)

--第一種 連接查詢
select a.ename Employee,a.hiredate EmpHiredate,
b.ename Manageer,b.hiredate "Mgr Hiredate"
from emp a,emp b
where a.mgr=b.empno and a.hiredate<b.hiredate

--第二種 子查詢
select c.ename Employee,c.hiredate EmpHiredate,
b.ename Manageer,b.hiredate "Mgr Hiredate"
from emp b,
(select ename ,hiredate,mgr from emp a 
where hiredate<(select hiredate from emp where empno=a.mgr)) c
where b.empno=c.mgr

9.顯示僱員姓名、年限工資(以New Salary作爲列標題)。年限工資的計算方法:從受僱日期起至今滿10年的,工資增加10%;滿20年的,工資增加20%,滿30年的,工資增加30%;其他的不變。

select ename,sal,hiredate,
decode(trunc(months_between(sysdate,hiredate)/120),
1,sal*1.1,2,sal*1.2,3,sal*1.3,sal) "New Salary"  
from emp

10.查詢出工資超過自己所在部門的平均工資的僱員號及工資(用兩種方法完成)。

--第一種  連接查詢
select  a.empno, a.sal
from emp a, 
(select deptno, avg(sal) avg from  emp group by deptno) b
WHERE   a.deptno = b.deptno  
AND     a.sal > b.avg

--第二種 子查詢
select ename,deptno,sal from emp a 
where sal>(
select avg(sal) from emp where deptno=a.deptno)

11.查詢所有部門中沒有僱員的部門名。

select distinct dept.dname
from dept,emp 
where dept.deptno not in 
(select distinct deptno from emp)

 

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