--查询员工和部门信息
select * from emp e,dept d where e.deptno=d.deptno
--查询员工姓名,部门名称
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno
--查询所有员工姓名,部门名称
select e.*, d.dname from emp e,dept d where e.deptno=d.deptno
--查询工资大于3000的员工姓名,工资和部门名称
select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptnoand e.sal>=3000
--非等值查询--查询公司工资等级select * from emp e,salgrands where e.sal<=s.hisal and e.sal>s.losal
select * from salagrade
--外链接--左外连接select*from emp e,dept d where e.deptno=d.deptno(+)
--右外连接
select*from emp e,dept d where e.deptno(+)=d.deptno
--自连接
--查询员工姓名和经理姓名
select e1.ename ,e2.ename from emp e1,emp e2 where e1.mgr=e2.deptno
--查询员工姓名、经理姓名和其他经理姓名
select e1.ename,e2.ename,e3.ename from emp e1,emp e2, emp e3
where e1.mgr=e2.empnoand e2.empno=e3.empno
**以上为92版仅在面试出现**
--SQL99 表连接--交叉连接 cross joinselect *from emp e crossjoin dept d
--自然连接 naturaljoinselect *from emp naturaljoin dept
--usingselect e.ename,deptno from emp e join dept d using(deptno)
--on 自定义连接
select *from emp e join dept d
on e.deptno=d.deptno
select *from emp e1 join emp e2 on e1.mgr=e2.empno
--查询员工的姓名、经理及其经理的名字
select e1.ename,e2.ename,e3.ename from emp e1 join emp e2 on e1.mgr=e2.empno join emp e3 on e2.mgr=e3.empno
--SQL99 innerjoin 两边都合法的数据
select*from emp e innerjoin dept d on e.deptno=d.deptno
--leftjoin 以左表为主
select*from emp e leftjoin dept d on e.deptno=d.deptno
--rightjoin 以右表为主
select*from emp e rightjoin dept d on e.deptno=d.deptno
--fulljoin 全连 取两个表的所有数据
select*from emp e fulljoin dept d on e.deptno=d.deptno
----查询员工的姓名、经理及其经理的名字和部门名字
select e1.ename, d.dname, e2.ename, d2.dname
from emp e1
leftjoin dept d
on e1.deptno = d.deptno
leftjoin emp e2
on e2.mgr = e2.empno
leftjoin dept d2
on d2.deptno = d2.deptno
**子查询**
--比CLARK工资高的人select SAL from emp where ename='CLARK'select *from emp where sal>(select SAL from emp where ename='CLARK')
--查询工资高于平均工资的雇员姓名和工资
selectavg(sal) from emp
select ename,sal from emp where sal>(selectavg(sal) from emp )
--查询同SCOTT同部门的且工资比他低的员工的名字和工资
select deptno from emp where ename='SCOTT'select sal from emp where ename='SCOTT'select ename, sal
from emp
where deptno = (select deptno from emp where ename = 'SCOTT')
and sal < (select sal from emp where ename = 'SCOTT')
--查询和'SMITH','SCOTT','CLARK'同一个部门的员工姓名
selectdistinct deptno from emp where ename in('SMITH','SCOTT','CLARK')
select *
from emp
where deptno in (selectdistinct deptno
from emp
where ename in ('SMITH', 'SCOTT', 'CLARK'))
--查询和'SMITH', 'SCOTT', 'CLARK'同一个部门并不包含他们三个的员工姓名
and ename notin ('SMITH', 'SCOTT', 'CLARK')
--查询工资最高的员工名字和工资
select ename,sal from emp where sal=(selectmax(sal)from emp )
--查询职务和'SCOTT'相同但是比'SCOTT'雇佣时间早的雇员信息
select * from emp
where job = (select job from emp where ename = 'SCOTT')
and hiredate < (select hiredate from emp where ename = 'SCOTT')
--查询工资比'SCOTT'高或者雇佣时间比'SCOTT'早的雇员编号和姓名
select empno,ename from emp
where job = (select job from emp where ename = 'SCOTT')
and hiredate < (select hiredate from emp where ename = 'SCOTT')
**多行子查询**
--查询工资低于任何一个“CLERK”的工资的雇员信息select*from emp where sal<any(select sal from emp where job='CLERK')
--查询工资比所有的“SALESMAN”都高的雇员的编号、名字和工资。
select empno, ename, sal
from emp
where sal > all (select sal from emp where job = 'SALESMAN')
--查询部门20中职务同部门10的雇员一样的雇员信息。
select *from emp where job in (select job from emp where deptno=10)and deptno=20
--查询在雇员中有哪些人是经理人
select *
from emp
where empno in (selectdistinct mgr
from emp
where mgr isnotnullor mgr != '')
--找出部门编号为20的所有员工中收入最高的职员
select ename
from emp
where sal = (selectmax(sal) from emp where deptno = 20)
and deptno = 20