Oracle学习笔记1

alter user scott account unlock; --解锁scott用户
select * from scott.emp where to_char(hiredate,'yyyy-mm-dd')>'1982-1-1'; --日期比较
select * from scott.emp where to_char(hiredate,'yyyy')='1980';  -- 入职时间年份为1980
select * from scott.emp where to_char(hiredate,'mm')='4'; -- 入职时间月份为4的员工
select * from scott.emp where sal between 800 and 1300;  --between是闭区间

like查询(模糊查询):
  %表示0到多个字符
  _表示1个任意字符
 in 的用法
 is null 的用法


select * from emp order by sal asc; --默认asc

 select * from scott.emp order by deptno,hiredate desc;

############################################################

nvl函数:
NVL( string1, replace_with)  
功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值。

select ename,sal*13+nvl(comm,0)*13 年薪 from scott.emp order by 年薪;
select ename,sal*13+nvl(comm,0)*13 年薪 from scott.emp order by sal*13+nvl(comm,0)*13;效率没

上面的高

############################################################
聚合函数:
max,min,avg,sum,count,
返回一个值的可以放在一起使用
select max(sal),min(sal) from scott.emp;
select avg(sal),sum(sal) from scott.emp;
!!!  avg不会统计值为Null的,如果要空值也考虑在呢,则:
select sum(comm)/count(*) from emp;
!!!  count(comm)统计时会忽略值为空的。
select ename,job from scott.emp where sal in (select max(sal) from scott.emp);

select语句执行的顺序:
1.写sql语句是从左到右,
2.sql执行在默认情况下是从右向左执行。(先排掉大量的数据)
3.不论select语句有多少,oracle有执行顺序。

 

############################################################

group by 对查询结果分组统计 
having子句 用于过滤分组显示结果,即对group by后的结果再进行过滤得到想要的结果
group by 与 having子句可以联合使用,

select avg(sal),max(sal),deptno from scott.emp group by deptno;

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

select avg(sal),deptno from scott.emp group by deptno having avg(sal)<2000;

若出现group by,having,order by则顺序按照group by,having,order by
 select avg(sal),deptno from scott.emp group by deptno having avg(sal)<2000 order by avg(sal)

desc;

 

小结:
1。分组函数(max,min,avg,sum,count..)只能出现在选择列表,having,order by 子句中,
2.如果在select语句中同时包含group by,having,order by,则它们的顺序是group by,having,order by
3.在选择列中,如果有列,表达式,和分组函数,那么这些列和表达式必须有一个出现在group by子句中

,否则就会出错。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值