查询入职日期最早的员工姓名,入职日期。
select e.ename,e.hiredate
from emp e
where e.hiredate=(select min(hiredate)
from emp)
查询工资比SMITH工资高并且工作地点在CHICAGO的员工姓名,工资,部门名称。
select aa.ename,aa.sal,dept.dname
from emp aa
join dept on aa.deptno=dept.deptno
where sal>(select sal
from emp
where ename='SMITH'
and loc ='CHICAGO')
查询入职日期比20部门入职日期最早的员工还要早的员工姓名,入职日期。
select aa.ename,aa.hiredate
from emp aa
where aa.hiredate<(select min(hiredate)
from emp
where deptno =20)
查询部门人数大于所有部门平均人数的部门编号,部门名称,部门人数。
select e.deptno,d.dname,bb
from emp e
join dept d on e.deptno=d.deptno
having count(e.empno) bb>(selec avg(count(empno)
aa)
from emp
group by deptno)
查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工。
select e.ename,e.hiredate
from emp e
where hiredate>any(select hiredate
from emp
where deptno =10)
and e.deptno<>10
查询入职日期比10部门所有员工晚的员工姓名、入职日期,不包括10部门员工。
select e.ename,e.hiredate
from emp e
where hiredate>all(select hiredate
from emp
where deptno =10)
and e.deptno<>10
查询职位和10部门任意一个员工职位相同的员 工姓名,职位,不包括10部门员工。
select e.ename,e.job
from emp e
where job in (select job
from emp
where deptno =10)
and e.deptno<>10
查询职位及经理和10部门任意一个员工职位及经理相同的员工姓名,职位,不包括10部门员工。
select e.ename,e.job
from emp e
where (job,mgr) in (select job,mgr
from emp
where deptno 10)
and e.deptno<>10
查询职位及经理和10部门任意一个员工职位或经理相同的员工姓名,职位,不包括10部门员工。
select ename, job
from emp
where job in
(select job
from emp
where deptno=10)
or mgr in
(select mgr
from emp
where deptno=10)
and deptno<>10
查询比自己职位平均工资高的员工姓名、职位,部门名称,职位平均工资。
select e.ename, e.job, d.dname, b.job_avgsal
from emp e
join dept d on e.deptno=d.deptno
join (select job, avg(sal) job_avgsal
from emp
group by job) b
on e.job=b.job
and e.sal>b.job_avgsal
查询职位和经理同员工SCOTT或BLAKE完全相同的员工姓名、职位,不包括SCOTT和BLAKE本人。
select e.ename, e.job
from emp e
where (e.job, e.mgr) in
(select job, mgr
from emp
where ename = 'SCOTT')
or
(e.job, e.mgr) in
(select job, mgr
from emp
where ename = 'BLAKE')
and e.ename not in ('SCOTT','BLAKE')
查询不是经理的员工姓名。
select e.ename
from emp e
where (select count(empno)
from emp
where e.empno=mgr)=0
查询入职日期最早的前5名员工姓名,入职日期
select rownum, ename,hiredate
from (select ename,hiredate
from emp
order by hiredate )
where rownum<=5
查询工作在CHICAGO并且入职日期最早的前2名的员工姓名,入职日期。
select rownum,ename,hiredate
FROM (select deptno, ename, hiredate
from emp
order by hiredate) e
join dept on emp.deptno=dept.deptno
where loc ='CHICAGO'
rownum <=2
按照每页显示5条记录,分别查询第1页,第2 页,第3页信息,要求显示员工姓名、入职日期 、部门名称。
select *
from (select rownum rn, e.ename, e.hiredate, d.dname
from emp e
join dept d on e.deptno=d.deptno
where rownum<=5)
where rn>0;
select *
from (select rownum rn, e.ename, e.hiredate, d.dname
from emp e
join dept d on e.deptno=d.deptno
where rownum<=10)
where rn>5;
select *
from (select rownum rn, e.ename, e.hiredate, d.dname
from emp e
join dept d on e.deptno=d.deptno
where rownum<=15)
where rn>10;
网友评论