一、连接查询
--笛卡尔积(表 * 表),连接的基础
select * from emp,dept;
--等值连接
select * from emp e,dept d where e.deptno = d.deptno;
--非等值连接
--eg:查询员工的薪水等级
select * from salgrade;
select e.ename,s.grade from emp e,salgrade s where
e.sal between s.losal and s.hisal;
--自连接 表自己连接自己,用两个别名区分
--eg:查询员工的上司的姓名:以“xxx的上司是xxx”方式显示
select e1.ename || '的上司是:' || e2.ename from emp e1,emp e2
where e1.mgr = e2.empno;
select * from emp;
--外连接
--eg:查询所有员工的部门名称,没有部门的也显示姓名
select e.ename,e.deptno from emp e,dept d
where e.deptno = d.deptno(+);
insert into emp (empno ,ename) values(40,'hsd%sshf')
select * from emp;
--sql 1999的链接类型
---cross join类型
select * from emp e cross join dept d;
---natural join
select * from emp e natural join dept d;
----inner join
--eg:查询员工的薪水等级
select * from salgrade;
select * from emp e inner join salgrade s on e.sal
between s.losal and s.hisal
--eg:查询员工的上司的姓名:以“xxx的上司是xxx”方式显示
select e1.empno || '的上司是:' || e2.empno from emp e1 inner join emp e2
on e1.mgr = e2.empno;
---外连接 left outer join
--eg:查询所有员工的部门名称,没有部门的也显示姓名
select * from emp e left outer join dept d on e.deptno = d.deptno;
--- right outer join
select * from dept d right outer join emp e on e.deptno = d.deptno;
---full outer join
select * from dept d full outer join emp e on e.deptno = d.deptno;
--多表查询
select 列名 from 表1 join 表2 on (表1,2连接条件)
join 表3 on(表1,3连接条件)或(表2,3连接条件)
--eg:查询员工的姓名、薪水、部门名称及工资等级
select e.ename,e.sal,d.dname,s.grade from emp e,dept d,salgrade s
where e.deptno = d.deptno and
e.sal between s.losal and s.hisal;
select e.ename,e.sal,d.dname,s.grade from salgrade s join emp e
on e.sal between s.losal and s.hisal join dept d
on e.deptno = d.deptno;
select e.ename, e.sal, d.dname, s.grade
from emp e
join dept d
on e.deptno = d.deptno
join salgrade s
on e.sal between s.losal and s.hisal;
select e.ename, e.sal, d.dname, s.grade
from emp e
join dept d
on e.deptno = d.deptno
join salgrade s
on e.sal between s.losal and s.hisal;
二、子查询
在from后加
在where后加