以scott/tiger登录oracle 9i这是oracle实例数据库,这里有三张表emp表,dept表,bonus表
emp表
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
dept表
DEPTNO DNAME LOC
------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
bonus表
BNUSNO LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
查询语句
1.员工和他的工资等级
select em.ename, bo.bnusno
from emp em, bonus bo
where em.sal > bo.losal
and em.sal < bo.hisal
--区间也可用 em.sal between bo.losal and bo.hisal
2.工资等级为4的员工所在的部门号和名称
select dp.deptno, dp.dname
from dept dp
where dp.deptno in (select em.deptno
from emp em, bonus bo
where em.sal between bo.losal and bo.hisal
and bo.bnusno = 4)
3.选择年终奖获奖者,他的工资比工资等级上限少出500元以上。
select em.ename,bo.hisal,em.comm
from emp em, bonus bo
where em.sal between bo.losal and bo.hisal
and em.comm>0
and bo.hisal-em.comm>500
4.找出这样的头儿,头儿的工资比员工的工资低
select distinct(ep1.ename) from emp ep1,emp ep2
where ep1.empno=ep2.mgr
and ep1.sal<ep2.sal
--ep1 经理表 ep2 员工表
5.找出 这样的员工,他的工资等级是他所在的部门中最高的
6.找出平均工资最高的部门的所有员工名,工资和工资等级
select emp.ename, emp.sal, bonus.bnusno
from emp, bonus
where emp.deptno = (select dt.deptno--查出平均工资最高值的部门号
from emp em, dept dt
where em.deptno = dt.deptno
group by dt.deptno
having avg(em.sal) = (select max(avg(b.sal))
from emp b
group by b.deptno))--查出平均工资最高值
and emp.sal between bonus.losal and bonus.hisal
7.请你查询出没有Manager的员工所在部门的所有员工;
select *
from emp
where emp.deptno in (select x.deptno from emp x where x.mgr is null)
8.部门号、部门名称和该部门的员工平均工资
select emp.deptno, dept.dname, avg(emp.sal)
from emp, dept
where emp.deptno = dept.deptno
group by emp.deptno,dept.dname
9.每个部门经理手下有多少员工
select count(emp.empno),(select e.ename from emp e where e.empno=emp.mgr),emp.mgr
from emp where mgr is not null
group by emp.mgr
11.每个部门经理手下有多少员工(部门经理的姓名和工作也要)
select b.empno,b.ename,b.job,count(*) as empCount from emp a,emp b where a.mgr=b.empno group by empno,b.ename,b.job;
12.参与每种工作的员工工资总额和comm平均值
select sum(sal),avg(comm),job from emp group by job;
13.请找出员工姓名和他的经理的经理
select x.empno,x.ename, y.empno, y.mgr//对应的是员工号,经理号,经理的经理号
from emp x, emp y
where x.mgr = y.empno
and y.mgr is not null//员工的经理可能已经没有经理的情况
14.找出没有员工的部门
内部查询返回多行多列(返回一张表)
select * from dept where not exists(select * from emp where emp.deptno=dept.deptno)
等价于
select * from dept where deptno not in(select deptno from emp where emp.deptno=dept.deptno)
等价于 右外链接
select dept.* from dept,emp
where dept.deptno = emp.deptno(+) and empno is null;
15.rownum是伪序列
(1).前5行(sal升序)
select * from emp where rownum<=5 order by sal
(2).后5行(sal升序)
select * from (select * from emp order by sal desc) //from 得到的是一个视图
where rownum<=5 order by sal;
(3).5到12行(sal升序)
select *
from (select emp.*, rownum rn from emp)//子查询得到伪序列
where rn <= 12
and rn >= 5