ENAME SAL GRADE
---------- ---------- ----------
SMITH 800 1
JAMES 950 1
ADAMS 1100 1
WARD 1250 2
MARTIN 1250 2
MILLER 1300 2
TURNER 1500 3
ALLEN 1600 3
CLARK 2450 4
BLAKE 2850 4
JONES 2975 4
SCOTT 3000 4
FORD 3000 4
KING 5000 5
14 rows selected.
idle>
练习:
1.查询名字是S开头的员工名,员工号,部门号,部门名,部位地理位置
select empno,ename,emp.deptno,dept.dname,dept.loc
from emp,dept
where ename like 'S%' and emp.deptno=dept.deptno;
2.查询员工名,部门名,工资,工资等级
select empno,ename,grade
from emp,salgrade
where sal between losal and hisal;
3.查询工作在NEW YORK的所有员工
select empno,ename,emp.deptno,dept.dname,dept.loc
from emp,dept
where emp.deptno=dept.deptno and dept.loc='NEW YORK';
自连接
同一张表内的连接查询 即把一个表映射成两个表
主要用于表的自参照关系 比如emp中的上下级或层次关系
因为自连接是同一张表之间的链接查询 所以必须定义表别名
例子:根据empno和mgr的对应关系,可以查找出KING的下属都有谁
idle> select b.ename||'''s manager is '||a.ename from emp a,emp b where a.empno = b.mgr and a.ename='KING';
B.ENAME||'''SMANAGERIS'||A.ENAME
----------------------------------
BLAKE's manager is KING
JONES's manager is KING
CLARK's manager is KING
idle>