oracle_DQL

select * from emp;

计算年薪:select sal*12 from emp;

起别名select ename, sal*12 annual_sal from emp;  如果要保持别名的空格、大小写等特征,别名加双引号:select ename, sal*12 "annual sal" from emp;

字符串:单引号 ‘happy’ 如果字符串中已经有单引号,则在串中有单引号的地方用两个单引号表示,如select 'It''s mine' from dual;

字符串连接:|| select'happy'||' birthday' from dual;

distinct 去重复 select distinct deptno from emp;

where  过滤 and、or、not、=、<、>、<>、between and

空值 is null 、is not null(任何含有NULL值的表达式结果都为空值)

in in() not in() 括号中是集合 select ename, sal, comm from emp where salin(800, 1200,1500);

日期 selectename, hiredate from emp where hiredate > '20-2-81';

like模糊查询,通配符 % 0个或多个 _1个 select enamefrom emp where ename like '_A%';

自定义转义字符如selectdeptno, dname from dept where dname like '%$%%' escape '$'; escape用于指定转义字符

orderby排序 desc asc select ename, sal from emp order by sal; 默认升序

select ename, sal from emp order by sal desc;

select ename,deptno,sal from emp order by deptno, saldesc;

groupby分组 select deptno, avg(sal) from emp group by deptno;

       select max(sal) from emp group by deptno,job;

在使用group by时,出现在select列表中的字段如果没有出现在组函数中,则必须出现在group by 子句中

having 可对分组后的记录进行过滤

       select avg(sal), deptno from emp group by deptno havingavg(sal) > 2000;

select avg(sal) from emp where sal > 1200 group bydeptno having avg(sal) > 1500 order by avg(sal) desc;

子查询 子查询可放在from或者where子句中

       select dname from dept where deptno in (select distinctdeptno from emp where sal > 3000);

表连接 等值连接、非等值连接、外连接(左外、右外、全外)

       等值连接 selectename, dname from emp join dept on(emp.deptno = dept.deptno);

                      select ename,dname from emp join dept using (deptno);

非等值连接select ename, grade from emp e joinsalgrade s on(e.sal between s.losal and s.hisal);

       外连接

左外 left join 或者 left outer join 左边表多余数据也会显示

       selectemp.ename, t.ename from emp left join emp t on(emp.mgr = t.empno);

右外 right join 或者 right outer join

       selectename, dname from emp right join dept on(emp.deptno = dept.deptno);

全外 full join

       笛卡尔积 cross join

              select ename, dname from emp cross join dept;

rownum是伪列,根据返回的记录生成一个序列化的数字 rownum只能和<、<=一块儿

select empno,ename from emp where rownum < 5;

 

select … from … join …on …  where … group by … having … orderby …

       连接条件放在join on中,过滤条件放在where中

 

示例:

☆     求所有人中薪水最高的那个人的薪水值

select max(sal) from emp;

☆     求薪水最高的人的名字

select ename from emp where sal = (select max(sal) fromemp);

☆     求每个部门挣钱最多的那个人的名字

select ename from emp where (deptno, sal) in(selectdeptno, max(sal + nvl(comm, 0)) from emp group by deptno);

☆     查询每个部门工资最高的人的姓名、薪水、部门号

select ename, sal, e.deptno from emp e join (selectmax(sal) max_sal, deptno from emp group by deptno) t on(e.sal = t.max_sal and e.deptno= t.deptno);

☆     求每个部门平均薪水的等级

select deptno, grade from salgrade s join (selectavg(sal) avg_sal, deptno from emp group by deptno) t on(t.avg_sal betweens.losal and s.hisal);

☆     求本人、经理人的名字

select e1.ename, e2.ename managerfrom emp e1 left join emp e2 on(e1.mgr = e2.empno);

☆     部门中哪些人的薪水最高

select empno,ename,deptno,sal

from emp

where (deptno,sal) in (select deptno, max(sal) from empgroup by deptno);

☆     求部门平均薪水的等级

select deptno, grade

from (select deptno, avg(sal) avg_sal from emp group bydeptno) t join salgrade s

on(t.avg_sal between s.losal and s.hisal);

☆     求部门平均的薪水等级

select deptno, avg(grade) from

(select empno, deptno, grade from emp join salgrade son(emp.sal between s.losal and s.hisal))

group by deptno;

☆     雇员中哪些人是经理人

select empno, dname from emp where empno in(selectdistinct mgr from emp);

☆     不用组函数,求薪水的最高值

select distinct sal from emp

where sal not in(select e1.sal from emp e1 join emp e2on(e1.sal < e2.sal));

☆     求平均薪水最高的部门的部门编号

select deptno, avg_sal from

(select deptno, avg(sal) avg_sal from emp group by deptno)

where avg_sal = 

(select max(avg_sal) from

(select deptno, avg(sal) avg_sal from empgroup by deptno)

);

--组函数嵌套

select deptno, avg_sal from

(select deptno, avg(sal) avg_sal from emp group bydeptno)

where avg_sal = (select max(avg(sal)) from emp group bydeptno);

☆     求平均薪水最高的部门的部门名称

select dname,avg_sal from dept join

(

select deptno,avg_sal from

        (selectdeptno, avg(sal) avg_sal from emp group by deptno)

where avg_sal =(select max(avg(sal)) from emp group by deptno)

) temp

on(dept.deptno = temp.deptno);

☆     求平均薪水的等级最低的部门的部门名称

select dname from dept where deptno =

(

select deptno from

(

        selectdeptno, grade from salgrade s join

        (

               selectdeptno, avg(sal) avg_sal from emp group by deptno

        ) ton(t.avg_sal between s.losal and s.hisal)

)

where grade =

(

        selectmin(grade) min_grade from

        (

               selectdeptno, grade from salgrade s join

               (

                      selectdeptno, avg(sal) avg_sal from emp group by deptno

               ) ton(t.avg_sal between s.losal and s.hisal)

        )

)

);

☆     求部门经理人中平均薪水最低的部门名称

select d.deptno, dname, avg_sal from dept d join

(

select deptno, avg(sal) avg_sal from

(

        selectdistinct e1.* from emp e1 join emp e2 on(e1.empno = e2.mgr)

)

group by deptno

) t on(d.deptno = t.deptno)

where t.avg_sal =

(

select min(avg_sal)from

(

        selectdeptno, avg(sal) avg_sal from

        (

               selectdistinct e1.* from emp e1 join emp e2 on(e1.empno = e2.mgr)

        )

        group bydeptno

)

);

☆     求比普通员工的最高薪水还要高的经理人名称

select ename from emp

where empno in(select distinct mgr from emp where mgr isnot null)

and sal >

(

select max(sal)from emp where empno not in

(

        selectdistinct mgr from emp where mgr is not null

)

);

☆     求薪水最高的前5名雇员

select * from (select * from emp order by sal desc) whererownum <= 5;

☆     求薪水最高的第6名到第10名雇员

select ename,sal from

(

select rownum r,t.*from

(select * from emporder by sal desc) t       

)

where r >5 and r < 11;

☆     比较效率

select * fromemp where deptno = 10 and ename like '%A%';

select * fromemp where ename like '%A%' and deptno = 10;

数字比字符串效率高,但实际运行的时候不一定,因为oracle可能会对sql语句做优化。

你可能感兴趣的:(oracle)