oracle练习-day01

--01.基础查询
--别名
select empno as "雇员编号",ename "雇员姓名",sal "123",job 职位 from emp;
--去重复
select distinct job from emp;
--四则运算
select ename,sal*12 yearly from emp;
--字符串拼接
select concat('a','b') from dual;
select 'a'||'b'||'c'||'d' from dual;

--02.条件查询
--查询没有奖金的员工
select * from emp where comm is null;
--查询工资大于1500小于3000的员工
select * from emp e where e.sal>1500 and e.sal<3000;
select * from emp e where e.sal between 1500 and 3000;
--查询工资小于1500的员工
select * from emp e where e.sal<1500;
--查询员工姓名为SMITH的
select * from emp e where e.ename='SMITH';
--查询员工编号是7788 7369 7566
select * from emp e where e.empno in(7788,7369,7566);

--03.模糊查询
--查询员工姓名带M的数据
select * from emp e where e.ename like '%M%';
--查询第二个字母为M的员工
select * from emp e where e.ename like '_M%';
--查询员工姓名带下划线_的员工
select * from emp e where e.ename like '%&_%' escape '&';
--04.排序
--按照奖金从高到低排序
select * from emp e order by comm desc nulls last;

--05.字符函数
--lower
select lower('AaBbCcDd')AaBbCcDd from dual;
--replace
select replace('he love you','he','i') test from dual;
--substr
select substr('13088888888',3,3) test from dual;
--concat
select concat('010-','88888888')||'转23' 高乾竞电话 from dual;
--length
select length('高乾竞'),length('北京市海淀区'),length('北京TO_CHAR') from dual;

--06.数值函数
--round
select round(5555.6666,3),round(5555.6666,-3),round(5555.5666) from dual;
--trunc
select trunc(5555.66666,2.1),trunc(5555.66666,-2.6),trunc(5555.033333)  from dual;
--mod
select mod(112,8),mod(24,8) from dual;

--07.日期函数
--计算每个员工的入职天数
select  ename,round((sysdate-e.hiredate)) days from emp e;
--计算入职员工的周数
select  ename,round((sysdate-e.hiredate)/7) weeks from emp e;
--计算入职员工的月数
select  ename,round((sysdate-e.hiredate)/30) months from emp e;
--一个月之后的时间
select sysdate,add_months(sysdate,1)  time from dual; 

--08.转化函数
--to_char
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;
select
to_char(sysdate,'d'), --每周第几天 
to_char(sysdate,'dd'), -- 每月第几天 
to_char(sysdate,'ddd'), -- 每年第几天 
to_char(sysdate,'ww'), -- 每年第几周 
to_char(sysdate,'mm'), -- 每年第几月 
to_char(sysdate,'q'), -- 每年第几季 
to_char(sysdate,'yyyy') -- 年 
from dual;
--to_date
select to_date('199912','yyyymm'),
to_date('2000.05.20','yyyy/mm/dd'),
(date '2008-12-31') XXdate, 
to_date('2008-12-31 12:31:30','yyyy-mm-dd hh24:mi:ss'),
(timestamp '2008-12-31 12:31:30') XXtimestamp
from dual;
--to_number
select to_number('11')+to_number('22') from dual;

--09.通用函数和条件表达式
--计算员工年薪
select ename,sal*12+nvl(comm,0) annual from emp;
--使用decode将职位转成中文显示
select e.ename,decode(job,'SALESMAN','销售','MANAGER','经理','其它') from emp e;
--使用case when then else end将职位转成中文显示
select e.ename,
case e.job
when 'SALESMAN' then '销售'
when 'MANAGER' then  '经理'
else  '其它'
end
from emp e;

--10.多行函数
--查询每个部门的人数
select deptno,count(*) from emp group by deptno;
--每个部门的最高工资
select deptno,max(sal) from emp group by deptno;
--每个部门的最低工资
select deptno,min(sal) from emp group by deptno;
--每个部门最高和最低的差距
select deptno,(max(sal)-min(sal)) from emp group by deptno;
--每个部门平均工资
select deptno,round(avg(sal)) from emp group by deptno;
--部门的平均工资大于2000的部门
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
--查询20号部门员工的工资总和
select sum(sal) from emp where deptno=20;

猜你喜欢

转载自www.cnblogs.com/coder-wf/p/12199047.html