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子句中
,否则就会出错。