Oracle学习及常见问题的解决-day02

day2

1.使用NVL(a,b)通用函数,统计员工年收入,NVL()作用于任何类型,即(number/varchar2/date)
通用函数:参数类型可以是number或varchar2或date类型
select ename,sal*12+NVL(comm,0) from emp;

使用NVL2(a,b,c)通用函数,如果a不为NULL,取b值,否则取c值,统计员工年收入
select ename,sal*12+NVL2(comm,comm,0) from emp;

使用NULLIF(a,b)通用函数,在类型一致的情况下(不一致则报错),如果a与b相同,返回NULL,否则返回a,比较10和10.0是否相同
select NULLIF(10,'10') from dual;


面试题:

使用SQL99标准通用语法中的case表达式,将职位是分析员的,工资+1000;职位是经理的,工资+800;职位是其它的,工资+400
case 字段
     when 条件1 then 表达式1
     when 条件2 then 表达式2
     else 表达式n
end
课后请参考<MySQL5.X的手册>-12.2这个章节
select ename "姓名",job "职位",sal "涨前工资",
       case job
        when 'ANALYST' then sal+1000
        when 'MANAGER' then sal+800
            else sal+400
       end "涨后工资"
from emp;

使用oracle专用语法中的decode()函数,职位是分析员的,工资+1000;职位是经理的,工资+800;职位是其它的,工资+400
decode(字段,条件1,表达式1,条件2,表达式2,...表达式n)
select ename "姓名",job "职位",sal "涨前工资",
       decode(job,'ANALYST',sal+1000,'MANAGER',sal+800,sal+400) "涨后工资"
from emp;


单引号出现的地方如下:
1)字符串,例如:'hello'
2)日期型,例如:'17-12月-80'
3)to_char/to_date(日期,'YYYY-MM-DD HH24:MI:SS')

双引号出现的地方如下:

1)列别名,例如:select ename "姓 名" from emp

2)to_char/to_date(日期,'YYYY"年"MM"月"DD"日" HH24:MI:SS')‘’号中的英文字符大小写不敏感


函数:oracle服务器先事写好的一段具有一定功能的程序片段,内置于oracle服务器,供用户调用
单行函数:输入一个参数,输出一个结果,例如:upper('baidu.com')->BAIDU.COM
多行函数:输入多个参数,或者是内部扫描多次,输出一个结果,例如:count(*)->14

统计emp表中员工总人数
select count(*) from emp;
*号适用于表字段较少的情况下,如果字段较多,扫描多间多,效率低,项目中提倡使用某一个非null唯一的字段,通常是主键

统计公司有多少个不重复的部门
select count(distinct deptno) from emp;

统计有佣金的员工人数
select count(comm) from emp;
注意:今天讲的这些多个行函数,不统计NULL值

员工总工资,平均工资,四舍五入,保留小数点后0位
select sum(sal) "总工资",round(avg(sal),0) "平均工资"
from emp;

查询员工表中最高工资,最低工资
select max(sal) "最高工资",min(sal) "最低工资"
from emp;

入职最早,入职最晚员工
select max(hiredate) "最晚入职时间",min(hiredate) "最早入职时间"
from emp;


按部门求出该部门平均工资,且平均工资取整数,采用截断

select deptno "部门编号",trunc(avg(sal),0) "部门平均工资"
from emp
group by deptno;

(继续)查询部门平均工资大于2000元的部门
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资"
from emp
group by deptno
having trunc(avg(sal),0) > 2000;

(继续)按部门平均工资降序排列
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资"
from emp
group by deptno
having trunc(avg(sal),0) > 2000
order by 2 desc;

除10号部门外,查询部门平均工资大于2000元的部门,方式一【having deptno<>10】
select deptno,avg(sal)
from emp
group by deptno
having deptno<>10;

除10号部门外,查询部门平均工资大于2000元的部门,方式二【where deptno<>10】
select deptno,avg(sal)
from emp
where deptno<>10
group by deptno;
提倡

显示部门平均工资的最大值
select max(avg(sal)) "部门平均工资的最大值"
from emp
group by deptno;

思考:显示部门平均工资的最大值和该部门编号?
select max(avg(sal)) "部门平均工资的最大值",deptno "部门编号"
from emp
group by deptno;
错误

group by 子句的细节:
1)在select子句中出现的非多行函数的所有列,【必须】出现在group by子句中

(deptno为非多行函数的列,必须要出现在group by子句中)

2)在group by子句中出现的所有列,【可出现可不现】在select子句中

where和having的区别:
where:

1)行过滤器
2)针对原始的记录
3)跟在from后面
4)where可省
5)先执行

having:
1)组过滤器
2)针对分组后的记录
3)跟在group by后面
4)having可省
5)后执行

oracle中综合语法:
1)select子句-----必须
2)from子句-------必须,不知写什么表了,就写dual
3)where子句------可选
4)group by子句---可选
5)having子句-----可选
6)order by 子句--可选,如果出现列名,别名,表达式,字段

内连接查询

员工表emp和部门表dept的笛卡尔集(笛卡尔集表=列数之和,行数之积,笛卡尔集表内中有些数据是不符合要求的)
select emp.ename,dept.dname
from emp,dept;

使用等值连接/内连接(只能使用=号),显示员工的编号,姓名,部门名,使用表别名简化
select emp.empno,emp.ename,dept.dname,dept.deptno
from emp,dept
where emp.deptno = dept.deptno;

使用非等值连接(不能使用=号,其它符号可以,例如:>=,<=,<>,betwen and等),显示员工的编号,姓名,月薪,工资级别
select e.empno,e.ename,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal;

内连接查询:只能查询出符合条件的记录


外连接查询

外连接查询:既能查询出符合条件的记录,也能根据一方强行将另一个方查询出来

使用外连接,按部门10,20,30,40号,统计各部门员工人数,要求显示部门号,部门名,人数
部门号 部门名        人数
10     ACCOUNTING    3
20     RESEARCH      5
30     SALES         6
40     OPERATIONS    0

等值连接/非等值连接/内连接:只会查询出多张表中,根据某个字段匹配,符合条件的记录,不符合条件的记录是不会存在的

左外连接[是oracle专用的,不是SQL99规则]:
select dept.deptno "部门号",dept.dname "部门名",count(emp.empno) "人数"
from dept,emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname;

右外连接:
select dept.deptno "部门号",dept.dname "部门名",count(emp.empno) "人数"
from dept,emp
where emp.deptno(+) = dept.deptno
group by dept.deptno,dept.dname;

使用左外连接,按部门10,20,30,40号,统计各部门员工人数,要求显示部门号,部门名,人数,且按人数降序排列
select dept.deptno "部门号",dept.dname "部门名",count(emp.empno) "人数"
from dept,emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname
order by 3 desc;

(可以用数字3代替列号,表示根据第三列进行排序)


自连接

使用自连接,显示"SMITH的上级是FORD"这种格式
select users.ename || '的上级是' ||boss.ename
from emp users,emp boss
where users.mgr = boss.empno;
只有13条记录,不含有KING

基于上述问题,将KING的上级是“”显示出来
select users.ename || '的上级是' ||boss.ename
from emp users,emp boss
where users.mgr = boss.empno(+);
14条记录
注意:自连接也用到内连接和外连接


子查询


子查询的作用:查询条件未知的事物

查询条件已知的问题:例如:查询工资为800的员工信息
查询条件未知的问题:例如:查询工资为20号部门平均工资的员工信息
一个条件未知的问题,可以分解为多个条件已知的问题

查询工资比WARD高的员工信息
第一:查询WARD的工资?
      select sal from emp where ename = 'WARD';

第二:查询工资比1250高的员工信息?
      select * from emp where sal > 1250;

子查询:
        select *
    from emp
    where sal > (
        select sal
        from emp
        where ename = 'WARD'
    );

查询部门名为'SALES'的员工信息(方式一:子查询)


第一:查询部门名为'SALES'的编号?
      select deptno from dept where dname = 'SALES';
第二:查询部门号为30的员工信息?
      select * from emp where deptno = 30;
子查询:
      select *
      from emp
      where deptno = (
      select deptno
      from dept
      where dname = 'SALES'
      );

子查询细节:
1)子查询与父查询可以针对同一张表
2)子查询与父查询可以针对不同张表
3) 子查询与父查询在传统参数时,数量要相同
4) 子查询与父查询在传统参数时,类型要相同
5) 子查询与父查询在传统参数时,含义要相同

查询部门名为'SALES'的员工信息(方式二:多表查询)

select emp.*
from dept,emp
where (dept.deptno=emp.deptno) and (dept.dname='SALES');

查询每个员工编号,姓名,部门名,工资等级(三表查询,这三张表并无外健关联)

select e.empno,e.ename,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 min(sal) from emp;
第二:查询工资为800的员工信息?
      select * from emp where sal = 800;
子查询:
      select *
      from emp
      where sal = (
            select min(sal)
            from emp
          );

查询部门名为'ACCOUNTING'或'SALES'的员工信息(多行子查询,使用in关键字)    
第一:查询部门名为'ACCOUNTING'或'SALES'的部门编号?
      select deptno from dept where dname in ('ACCOUNTING','SALES');
第二:查询部门号为10或30号的员工信息?
      select * from emp where deptno in (10,30);
子查询:
      select *
      from emp
      where deptno in (
               select deptno
               from dept
                       where dname in ('ACCOUNTING','SALES')
                      );

查询工资比20号部门【任意any】一个员工工资【低<】的员工信息(多行子查询,使用any关键字)
第一:查询20号部门的所有工资?
      select sal from emp where deptno = 20;
第二:查询工资比(800,2975,3000,1100,3000)任意一个低的员工信息?
      select * from emp where sal < any (800,2975,3000,1100,3000);   
在oracle看来,<any就等于<集合中最大的那个值(在Mysql中也一样适用)
子查询:
      select *
      from emp
      where sal <any (
            select sal
            from emp
            where deptno = 20
              );

查询工资比30号部门【所有all】员工【低<】的员工信息(多行子查询,使用all关键字)

第一:查询出30部门所有员工的工资?    
      select sal from emp where deptno = 30;
第二:查询工资比(1600,1250,1250,2850,1500,950)中所有的工资都低的员工信息?
      select * from emp where sal <all (1600,1250,1250,2850,1500,950);
子查询:
      select *
      from emp
      where sal <all (
            select sal
            from emp
            where deptno = 30
              );

注意:不容易理解的几个概念:

单行函数:输入一个参数,输出一个结果
多行函数:扫描多个参数,输出一个结果

单行子查询:子查询只会返回一个结果,例如:800,父查询用=/<>/>=/<=这些符号来比较
多行子查询:子查询会返回多于一个结果,例如:30,20,父查询用in/any/all这些符号来比较

当多表查询,子查询同时能解决问题时,按如下优先方案选择:

多表查询>子查询
注意:上述结果不是说多表查询可以替代子查询,某些情况下,只能用子查询解决,例如:oracle分页


集合查询

使用并集运算,查询20号部门或30号部门的员工信息
select * from emp where deptno = 20
union
select * from emp where deptno = 30;
注意:
union:二个集合中,如果都有相同的,取其一
union all:二个集合中,如果都有相同的,都取

//使用这两个方法可以看到数据库语句或命令的执行时间
使用set time/timing on,打开时间的开关
set time on;
set time off;

使用set tim/timing off,关闭时间的开关
set timing on;
set timint off;

使用交集运算[intersect],查询工资在1000-2000和1500-2500之间的员工信息(方式一)
select * from emp where sal between 1000 and 2000
intersect
select * from emp where sal between 1500 and 2500;

用where行过滤,查询工资在1000-2000和1500-2500之间的员工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal between 1500 and 2500);

使用差集运算[minus],查询工资在1000-2000,但不在1500-2500之间的员工信息(方式一)
select * from emp where sal between 1000 and 2000
minus
select * from emp where sal between 1500 and 2500;

使用where行过滤,查询工资在1000-2000,但不在1500-2500之间的员工信息(方式二)

select *
from emp
where (sal between 1000 and 2000) and (sal not between 1500 and 2500);

集合查询的细节:
1)集合操作时,必须确保集合列数是相等
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 30;错

2)集合操作时,必须确保集合列类型对应相同
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal,hiredate from emp where deptno = 30;错

3)A union B union C = C union B union A
select * from emp where deptno = 10
union
select * from emp where deptno = 20
union
select * from emp where deptno = 30;

4)当多个集合操作时,结果的列名由第一个集合列名决定
select empno "编号",ename "姓名",sal "薪水" from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 10;

当多表查询,子查询,集合查询都能完成同样任务时,按如下优化方案选择:
多表查询->子查询->集合查询






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值