多表连接:
等值连接
select emp.empno,emp.ename,emp.deptno,dept.deptno,dept.loc from emp,dept where emp.deptno=dept.deptno
迪卡儿连接:
select * from emp,dept;
取出emp表中工资介于salgrade表中最低和最高之间的记录。
select e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;
外连接:
取出所有员工所属部门名,若部门无员工,则表示为空。
select e.ename,d.deptno,d.dname from emp e,dept d where e.deptno(+)=d.deptno order by e.deptno;
自连接:
找出员工的上级关系
select worker.ename||' word for '||manager.ename from emp worker,emp manager where worker.mgr=manager.empno;
聚合函数:
select avg(sal),max(sal),min(sal),sum(sal) from emp where job like 'SALES%';
select min(hiredate),max(hiredate) from emp;
select count(*) from emp where deptno=30;
select count(comm) from emp where deptno=30;
有奖金的人的平均奖金
select avg(comm) from emp;
所有人的平均奖金
select avg(nvl(comm,0)) from emp;
求每个部门的平均薪水:
select emp.deptno,dept.dname,avg(sal) from emp,dept where emp.deptno=dept.deptno group by emp.deptno,dept.dname;
group by用法:
按部门分组:每月的平均工资、总工资、最高工资、最低工资
select deptno,avg(sal),sum(sal),max(sal),min(sal) from emp group by deptno;
按部门分组:每种职位的薪水和(按同工的人数来加)
select deptno,job,sum(sal) from emp group by deptno,job;
找出职位工资和大于5000,且不以SALES开头的工作及其工资之和。
select job,sum(sal) PAYROLL from emp where job not like 'SALES%' group by job having sum(sal)>5000 order by sum(sal);
子查询:
单行子查询
select ename from emp where sal>(select sal from emp where empno=7566);
select ename,job from emp where job=(select job from emp where empno=7369) and sal>(select sal from emp where empno=7876);
多行子查询
select empno,ename,job from emp where sal<any(select sal from emp where job='CLERK') and job<>'CLERK';
select empno,ename,job from emp where sal>all(select avg(sal) from emp group by deptno);