select deptno from dept;//无重复(union 与union all的区别)
select e.ename, d.loc from emp e, dept d where e.deptno=d.deptno;
select e.ename, d.loc from emp e inner join dept d on(e.deptno=d.deptno);
create table v as (select ename, job from emp where job='clerk');
select e.ename, e.job, e.sal from emp e, v where e.ename=v.ename and e.job=v.job;
select e.ename, e.job, e.sal from emp e inner join v on(e.ename=v.ename and e.job=v.job);
select ename, job, sal from emp where(ename,job) in (select ename, job from v);
select deptno from dept where deptno not in (select deptno from emp);
//当(select deptno from emp)中出现null值是会出现错误
select d.deptno from dept d
where not exists (select null from emp e where d.deptno=e.deptno);
select d.* from dept d left outer join emp e on(d.deptno=e.deptno) where e.deptno is null;
select d.* from dept d where d.deptno not in(select deptno from emp);
select e.ename, d.loc, eb.received from
emp e join dept d on (e.deptno=d.deptno)
left join emp_bonus eb on(e.empno=eb.empno)
order by d.loc; //内联加外联
select e.ename, d.loc,
(select eb.received from emp_bonus eb where eb.empno=e.empno) as r
from emp e, dept d where e.deptno=d.deptno
order by 2; //使用标量子查询解决外联的性能问题
create view v as
select ename,job from emp where deptno!=10
union all
select ename,job from emp where ename='ward';
select * from (select e.ename,e.job,count(*) as cnt from emp e group by ename,job) e where not exists(select null from (select v.ename,v.job,count(*) as cnt from v group by ename,job) v where e.ename=v.ename and e.job=v.job and e.cnt=v.cnt )
union all
select * from(select v.ename,v.job,count(*) as cnt from v group by ename,job) v where not exists( select null from(select e.ename,e.job,count(*) as cnt from emp e group by ename,job)e where v.ename=v.ename and v.job=e.job and v.cnt=e.cnt);
select e.empno,e.ename,e.sal* case when eb.type=1 then 0.1 when eb.type=2 then 0.2 when eb.type=3 then 0.3 else 0 end as bonus from emp e, emp_bonus eb where e.empno=eb.empno;
select empno, ename, sum(bonus) as total_bonus from (select e.empno,e.ename,e.sal* case when eb.type=1 then 0.1 when eb.type=2 then 0.2 when eb.type=3 then 0.3 else 0 end as bonus from emp e, emp_bonus eb where e.empno=eb.empno) group by empno, ename;