- 时间:2021-04-01 16:26 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:oracle常用sql查询语句部分集合(图文)
[b]Oracle[/b][b]查询语句[/b]
select * from scott.emp ;
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image001.jpg[/img]
[i]1.--dense_rank()[/i][i]分析函数(查找每个部门工资最高前三名员工信息)[/i]
select * from (select deptno,ename,sal,dense_rank() over(partition by deptno order by sal desc) a from scott.emp) where a<=3 order by deptno asc,sal desc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image002.jpg[/img]
[i]--rank()[/i][i]分析函数(运行结果与上语句相同)[/i]
select * from (select deptno,ename,sal,rank() over(partition by deptno order by sal desc) a from scott.emp ) where a<=3 order by deptno asc,sal desc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image003.jpg[/img]
[i]--row_number()[/i][i]分析函数(运行结果与上相同)[/i]
select * from(select deptno,ename,sal,row_number() over(partition by deptno order by sal desc) a from scott.emp) where a<=3 order by deptno asc,sal desc ;
[i]--rows unbounded preceding [/i][i]分析函数(显示各部门的积累工资总和)[/i]
select deptno,sal,sum(sal) over(order by deptno asc rows unbounded preceding) 积累工资总和 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image004.jpg[/img]
[i]--rows [/i][i]整数值 preceding(显示每最后4条记录的汇总值)[/i]
select deptno,sal,sum(sal) over(order by deptno rows 3 preceding) 每4汇总值 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image005.jpg[/img]
[i]--rows between 1 preceding and 1 following[/i][i](统计3条记录的汇总值【当前记录居中】)[/i]
select deptno,ename,sal,sum(sal) over(order by deptno rows between 1 preceding and 1 following) 汇总值 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image006.jpg[/img]
[i]--ratio_to_report([/i][i]显示员工工资及占该部门总工资的比例)[/i]
select deptno,sal,ratio_to_report(sal) over(partition by deptno) 比例 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image007.jpg[/img]
[i]--[/i][i]查看所有用户[/i]
select * from dba_users ;
select count(*) from dba_users ;
select * from all_users ;
select * from user_users ;
select * from dba_roles ;
[i]--[/i][i]查看用户系统权限[/i]
select * from dba_sys_privs ;
select * from user_users ;
[i]--[/i][i]查看用户对象或角色权限[/i]
select * from dba_tab_privs ;
select * from all_tab_privs ;
select * from user_tab_privs ;
[i]--[/i][i]查看用户或角色所拥有的角色[/i]
select * from dba_role_privs ;
select * from user_role_privs ;
[i]-- rownum:[/i][i]查询10至12信息[/i]
select * from scott.emp a where rownum<=3 and a.empno not in(select b.empno from scott.emp b where rownum<=9);
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image008.jpg[/img]
[i]--not exists[/i][i];查询emp表在dept表中没有的数据[/i]
select * from scott.emp a where not exists(select * from scott.dept b where a.empno=b.deptno) ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image009.jpg[/img]
[i]--rowid;[/i][i]查询重复数据信息[/i]
select * from scott.emp a where a.rowid>(select min(x.rowid) from scott.emp x where x.empno=a.empno);
[i]--[/i][i]根据rowid来分页(一万条数据,查询10000至9980时间大概在0.03秒左右)[/i]
select * from scott.emp where rowid in(select rid from(select rownum rn,rid from(select rowid rid,empno from scott.emp order by empno desc) where rownum<10)where rn>=1)order by empno desc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image010.jpg[/img]
[i]--[/i][i]根据分析函数分页(一万条数据,查询10000至9980时间大概在1.01秒左右)[/i]
select * from(select a.*,row_number() over(order by empno desc) rk from scott.emp a ) where rk<10 and rk>=1;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image011.jpg[/img]
[i]--rownum[/i][i]分页(一万条数据,查询10000至9980时间大概在0.01秒左右)[/i]
select * from(select t.*,rownum rn from(select * from scott.emp order by empno desc)t where rownum<10) where rn>=1;
select * from(select a.*,rownum rn from (select * from scott.emp) a where rownum<=10) where rn>=5 ;
[i]--left outer join:[/i][i]左连接[/i]
select a.*,b.* from scott.emp a left outer join scott.dept b on a.deptno=b.deptno ;
[i]--right outer join:[/i][i]右连接[/i]
select a.*,b.* from scott.emp a right outer join scott.dept b on a.deptno=b.deptno ;
[i]--inner join[/i]
select a.*,b.* from scott.emp a inner join scott.dept b on a.deptno=b.deptno ;
[i]--full join [/i]
select a.*,b.* from scott.emp a full join scott.dept b on a.deptno=b.deptno ;
select a.*,b.* from scott.emp a,scott.dept b where a.deptno(+)=b.deptno ;
select distinct ename,sal from scott.emp a group by sal having ;
select * from scott.dept ;
select * from scott.emp ;
[i]--case when then end ([/i][i]交叉报表)[/i]
select ename,sal,case deptno when 10 then '会计部' when 20 then '研究部' when 30 then '销售部' else '其他部门' end 部门 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image012.jpg[/img]
select ename,sal,case when sal>0 and sal<1500 then '一级工资' when sal>=1500 and sal<3000 then '二级工资' when sal>=3000 and sal<4500 then '三级工资' else '四级工资' end 工资等级 from scott.emp order by sal desc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image013.jpg[/img]
[i]--[/i][i]交叉报表是使用分组函数与case结构一起实现[/i]
select 姓名,sum(case 课程 when '数学' then 分数 end)数学,sum(case 课程 when '历史' then 分数 end)历史 from 学生 group by 姓名 ;
[i]--decode [/i][i]函数[/i]
select 姓名,sum(decode(课程,'数学',分数,null))数学,sum(decode(课程,'语文',分数,null))语文,sum(decode(课程,'历史','分数',null))历史 from 学生 group by 姓名 ;
[i]--level[/i][i]。。。。connect by(层次查询)[/i]
select level,emp.* from scott.emp connect by prior empno = mgr order by level ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image014.jpg[/img]
[i]--sys_connect_by_path[/i][i]函数[/i]
select ename,sys_connect_by_path(ename,'/') from scott.emp start with mgr is null connect by prior empno=mgr ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image015.jpg[/img]
[i]--start with connect by prior [/i][i]语法[/i]
select lpad(ename,3*(level),'')姓名,lpad(ename,3*(level),'')姓名 from scott.emp where job<>'CLERK' start with mgr is null connect by prior mgr = empno ;
[i]--level[/i][i]与prior关键字[/i]
select level,emp.* from scott.emp start with ename='SCOTT' connect by prior empno=mgr;
select level,emp.* from scott.emp start with ename='SCOTT' connect by empno = prior mgr ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image016.jpg[/img]
[i]--[/i][i]等值连接[/i]
select empno,ename,job,sal,dname from scott.emp a,scott.dept b where a.deptno=b.deptno and (a.deptno=10 or sal>2500);
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image017.jpg[/img]
[i]--[/i][i]非等值连接[/i]
select a.ename,a.sal,b.grade from scott.emp a,scott.salgrade b where a.sal between b.losal and b.hisal ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image018.jpg[/img]
[i]--[/i][i]自连接[/i]
select a.ename,a.sal,b.ename from scott.emp a,scott.emp b where a.mgr=b.empno ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image019.jpg[/img]
[i]--[/i][i]左外连接[/i]
select a.ename,a.sal,b.ename from scott.emp a,scott.emp b where a.mgr=b.empno(+);
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image020.jpg[/img]
[i]--[/i][i]多表连接[/i]
select * from scott.emp ,scott.dept,scott.salgrade where scott.emp.deptno=scott.dept.deptno and scott.emp.sal between scott.salgrade.losal and scott.salgrade.hisal ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image021.jpg[/img]
select * from scott.emp a join scott.dept b on a.deptno=b.deptno join scott.salgrade s on a.sal between s.losal and s.hisal where a.sal>1000;
select * from(select * from scott.emp a join scott.dept b on a.deptno=b.deptno where a.sal>1000) c join scott.salgrade s on c.sal between s.losal and s.hisal ;
[i]--[/i][i]单行子查询[/i]
select * from scott.emp a where a.deptno=(select deptno from scott.dept where loc='NEW YORK');
select * from scott.emp a where a.deptno in (select deptno from scott.dept where loc='NEW YORK');
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image022.jpg[/img]
[i]--[/i][i]单行子查询在 from 后[/i]
select scott.emp.*,(select deptno from scott.dept where loc='NEW YORK') a from scott.emp ;
[i]--[/i][i]使用 in ,all,any 多行子查询[/i]
[i]--in[/i][i]:表示等于查询出来的对应数据[/i]
select ename,job,sal,deptno from scott.emp where job in(select distinct job from scott.emp where deptno=10);
[i]--all:[/i][i]表示大于所有括号中查询出来的对应的数据信息[/i]
select ename,sal,deptno from scott.emp where sal>all(select sal from scott.emp where deptno=30);
[i]--any:[/i][i]表示大于括号查询出来的其中任意一个即可(只随机一个)[/i]
select ename,sal,deptno from scott.emp where sal>any(select sal from scott.emp where deptno=30);
[i]--[/i][i]多列子查询[/i]
select ename,job,sal,deptno from scott.emp where(deptno,job)=(select deptno,job from scott.emp where ename='SCOTT');
select ename,job,sal,deptno from scott.emp where(sal,nvl(comm,-1)) in(select sal,nvl(comm,-1) from scott.emp where deptno=30);
[i]--[/i][i]非成对比较[/i]
select ename,job,sal,deptno from scott.emp where sal in(select sal from scott.emp where deptno=30) and nvl(comm,-1) in(select nvl(comm,-1) from scott.emp where deptno=30);
[i]--[/i][i]其他子查询[/i]
select ename,job,sal,deptno from scott.emp where exists(select null from scott.dept where scott.dept.deptno=scott.emp.deptno and scott.dept.loc='NEW YORK');
select ename,job,sal from scott.emp join(select deptno,avg(sal) avgsal,null from scott.emp group by deptno) dept on emp.deptno=dept.deptno where sal>dept.avgsal ;
create table scott.test(
ename varchar(20),
job varchar(20)
);
[i]--drop table test ;[/i]
select * from scott.test ;
[i]--Insert[/i][i]与子查询(表间数据的拷贝)[/i]
insert into scott.test(ename,job) select ename,job from scott.emp ;
[i]--Update[/i][i]与子查询[/i]
update scott.test set(ename,job)=(select ename,job from scott.emp where ename='SCOTT' and deptno ='10');
[i]--[/i][i]创建表时,还可以指定列名[/i]
create table scott.test_1(ename,job) as select ename,job from scott.emp ;
select * from scott.test_1 ;
[i]--delete[/i][i]与子查询[/i]
delete from scott.test where ename in('');
[i]--[/i][i]合并查询[/i]
[i]--union[/i][i]语法(合并且去除重复行,且排序)[/i]
select ename,sal,deptno from scott.emp where deptno>10 union select ename,sal,deptno from scott.emp where deptno<30 ;
select a.deptno from scott.emp a union select b.deptno from scott.dept b ;
[i]--union all([/i][i]直接将两个结果集合并,不排序)[/i]
select ename,sal,deptno from scott.emp where deptno>10 union all select ename,sal,deptno from scott.emp where deptno<30 ;
select a.deptno from scott.emp a union all select b.deptno from scott.dept b ;
[i]--intersect:[/i][i]取交集[/i]
select ename,sal,deptno from scott.emp where deptno>10 intersect select ename,sal,deptno from scott.emp where deptno<30;
[i]--[/i][i]显示部门工资总和高于雇员工资总和三分之一的部门名及工资总和[/i]
select dname as 部门,sum(sal) as 工资总和 from scott.emp a,scott.dept b where a.deptno=b.deptno group by dname having sum(sal)>(select sum(sal)/3 from scott.emp c,scott.dept d where c.deptno=d.deptno);
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image023.jpg[/img]
[i]--[/i][i]使用with得到以上同样的结果[/i]
with test as (select dname ,sum(sal) sumsal from scott.emp ,scott.dept where scott.emp.deptno=scott.dept.deptno group by dname) select dname as 部门,sumsal as 工资总和 from scott.test where sumsal>(select sum(sumsal)/3 from scott.test);
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image024.jpg[/img]
[i]--[/i][i]分析函数[/i]
select ename,sal,sum(sal) over(partition by deptno order by sal desc) from scott.emp ;
[i]--rows n preceding([/i][i]窗口子句一)[/i]
select deptno,sal,sum(sal) over(order by sal rows 5 preceding) from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image025.jpg[/img]
[i]--rum(..) over(..)..[/i]
select sal,sum(1) over(order by sal) aa from scott.emp ;
select deptno,ename,sal,sum(sal) over(order by ename) 连续求和,sum(sal) over() 总和,100*round(sal/sum(sal) over(),4) as 份额 from scott.emp;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image026.jpg[/img]
select deptno,ename,sal,sum(sal) over(partition by deptno order by ename) 部门连续求和,sum(sal) over(partition by deptno) 部门总和,100*round(sal/sum(sal) over(),4) as 总份额 from scott.emp;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image027.jpg[/img]
select deptno,sal,rank() over (partition by deptno order by sal),dense_rank() over(partition by deptno order by sal) from scott.emp order by deptno ;
结果;
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image028.jpg[/img]
select * from (select rank() over(partition by 课程 order by 分数 desc) rk,分析函数_rank.* from 分析函数_rank) where rk<=3 ;
[i]--dense_rank():[/i][i]有重复的数字不跳着排列[/i]
[i]--row_number()[/i]
select deptno,sal,row_number() over(partition by deptno order by sal) rm from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image029.jpg[/img]
[i]--lag()[/i][i]和lead()[/i]
select deptno,sal,lag(sal) over(partition by deptno order by sal) 上一个,lead(sal) over(partition by deptno order by sal) from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image030.jpg[/img]
[i]--max(),min(),avg()[/i]
select deptno,sal,max(sal) over(partition by deptno order by sal)最大,min(sal) over(partition by deptno order by sal)最小,avg(sal) over(partition by deptno order by sal)平均 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image031.jpg[/img]
[i]--first_value(),last_value()[/i]
select deptno,sal,first_value(sal) over(partition by deptno)最前,last_value(sal) over(partition by deptno )最后 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image032.jpg[/img]
[i]--[/i][i]分组补充 group by grouping sets[/i]
select deptno ,sal,sum(sal) from scott.emp group by grouping sets(deptno,sal);
select null,sal,sum(sal) from scott.emp group by sal union all select deptno,null,sum(sal) from scott.emp group by deptno ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image033.jpg[/img]
[i]--rollup[/i]
select deptno,job,avg(sal) from scott.emp group by rollup(deptno,job) ;
[i]--[/i][i]理解rollup等价于[/i]
select deptno,job,avg(sal) from scott.emp group by deptno,job union select deptno ,null,avg(sal) from scott.emp group by deptno union select null,null,avg(sal) from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image034.jpg[/img]
select deptno,job,avg(sal) a from scott.emp group by cube(deptno,job) ;
[i]--[/i][i]理解CUBE[/i]
select deptno,job,avg(sal) from scott.emp group by cube(deptno,job) ;
[i]--[/i][i]等价于[/i]
select deptno,job,avg(sal) from scott.emp group by grouping sets((deptno,job),(deptno),(job),());
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image035.jpg[/img]
[i]--[/i][i]查询工资不在1500至2850之间的所有雇员名及工资[/i]
select ename,sal from scott.emp where sal not in(select sal from scott.emp where sal between 1500 and 2850 );
[i]--[/i][i]部门10和30中的工资超过1500的雇员名及工资[/i]
select deptno,ename,sal from scott.emp a where a.deptno in(10,30) and a.sal>1500 order by sal desc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image036.jpg[/img]
[i]--[/i][i]在1981年2月1日至1981年5月1日之间雇佣的雇员名,岗位及雇佣日期,并以雇佣日期先后顺序排序[/i]
select ename as 姓名,job as 岗位,hiredate as 雇佣日期 from scott.emp a where a.hiredate between to_date('1981-02-01','yyyy-mm-dd') and to_date('1981-05-01','yyyy-mm-dd') order by a.hiredate asc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image037.jpg[/img]
select * from scott.emp where hiredate >to_date('1981-02-01','yyyy-MM-dd');
[i]--[/i][i]查询获得补助的所有雇佣名,工资及补助额,并以工资和补助的降序排序[/i]
select ename,sal,comm from scott.emp a where a.comm > all(0) order by comm desc;
[i]--[/i][i]工资低于1500的员工增加10%的工资,工资在1500及以上的增加5%的工资并按工资高低排序(降序)[/i]
select ename as 员工姓名,sal as 补发前的工资,case when sal<1500 then (sal+sal*0.1) else (sal+sal*0.05) end 补助后的工资 from scott.emp order by sal desc ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image038.jpg[/img]
[i]--[/i][i]查询公司每天,每月,每季度,每年的资金支出数额[/i]
select sum(sal/30) as 每天发的工资,sum(sal) as 每月发的工资,sum(sal)*3 as 每季度发的工资,sum(sal)*12 as 每年发的工资 from scott.emp;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image039.jpg[/img]
[i]--[/i][i]查询所有员工的平均工资,总计工资,最高工资和最低工资[/i]
select avg(sal) as 平均工资,sum(sal) as 总计工资,max(sal) as 最高工资,min(sal) as 最低工资 from scott.emp;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image040.jpg[/img]
[i]--[/i][i]每种岗位的雇员总数和平均工资[/i]
select job as 岗位,count(job) as 岗位雇员总数,avg(sal) as 平均工资 from scott.emp group by job order by 平均工资 desc;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image041.jpg[/img]
[i]--[/i][i]雇员总数以及获得补助的雇员数[/i]
select count(*) as 公司雇员总数,count(comm) as 获得补助的雇员人数 from scott.emp ;
[i]--[/i][i]管理者的总人数[/i]
[i]--[/i][i]雇员工资的最大差额[/i]
select max(sal),min(sal),(max(sal) - min(sal)) as 员工工资最大差额 from scott.emp ;
[i]--[/i][i]每个部门的平均工资[/i]
select deptno,avg(sal) from scott.emp a group by a.deptno;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image042.jpg[/img]
[i]--[/i][i]查询每个岗位人数超过2人的所有职员信息[/i]
select * from scott.emp a,(select c.job,count(c.job) as sl from scott.emp c group by c.job ) b where b.sl>2 and a.job=b.job;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image043.jpg[/img]
select * from scott.emp a where a.empno in(select mgr from scott.emp ) and (select count(mgr) from scott.emp)>2 ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image044.jpg[/img]
[i]--[/i][i]处理重复行数据信息(删除,查找,修改)[/i]
select * from a1 a where not exists (select b.rd from (select rowid rd,row_number() over(partition by LOAN, BRANCH order by BEGIN_DATE desc) rn from a1) b where b.rn = 1 and a.rowid = b.rd);
[i]--[/i][i]查询emp表数据信息重复问题[/i]
select * from scott.emp a where exists(select b.rd from(select rowid rd,row_number() over(partition by ename,job,mgr,hiredate,sal,comm,deptno order by empno asc) rn from scott.emp) b where b.rn=1 and a.rowid=b.rd);
[i]--initcap[/i][i]:返回字符串,字符串第一个字母大写[/i]
select initcap(ename) Upp from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image045.jpg[/img]
[i]--ascii:[/i][i]返回与指定的字符对应的十进制数[/i]
select ascii(a.empno) as 编号,ascii(a.ename) as 姓名,ascii(a.job) as 岗位 from scott.emp a ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image046.jpg[/img]
[i]--chr:[/i][i]给出整数,返回对应的字符[/i]
select chr(ascii(ename)) as 姓名 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image047.jpg[/img]
[i]--concat:[/i][i]连接字符串[/i]
select concat(a.ename,a.job)|| a.empno as 字符连接 from scott.emp a;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image048.jpg[/img]
[i]--instr:[/i][i]在一个字符串中搜索指定的字符,返回发现指定的字符的位置[/i]
select instr(a.empno,a.mgr,1,1) from scott.emp a ;
[i]--length:[/i][i]返回字符串的长度[/i]
select ename,length(a.ename) as 长度,a.job,length(a.job) as 长度 from scott.emp a ;
[i]--lower[/i][i]:返回字符串,并将所返回的字符小写[/i]
select a.ename as 大写,lower(a.ename) as 小写 from scott.emp a ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image049.jpg[/img]
[i]--upper[/i][i]:返回字符串,并将返回字符串都大写[/i]
select lower(a.ename) as 小写名字,upper(a.ename) as 大写名字 from scott.emp a ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image050.jpg[/img]
[i]--rpad:[/i][i]在列的右边粘贴字符,lpad: 在列的左边粘贴字符(不够字符则用*来填满)[/i]
select lpad(rpad(a.ename,10,'*'),16,'*') as 粘贴 from scott.emp a ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image051.jpg[/img]
[i]--like[/i][i]不同角度的使用[/i]
select * from scott.emp where ename like '%XXR%';
select * from scott.emp where ename like '%S';
select * from scott.emp where ename like 'J%';
select * from scott.emp where ename like 'S';
select * from scott.emp where ename like '%S_';
[i]--[/i][i]每个部门的工资总和[/i]
select a.ename,sum(sal) from scott.emp a group by ename;
[i]--[/i][i]每个部门的平均工资[/i]
select a.deptno,avg(sal) from scott.emp a group by deptno ;
[i]--[/i][i]每个部门的最大工资[/i]
select a.deptno,max(sal) from scott.emp a group by deptno ;
[i]--[/i][i]每个部门的最小工资[/i]
select a.deptno,min(sal) from scott.emp a group by deptno ;
[i]--[/i][i]查询原工资占部门工资的比率[/i]
select deptno ,sal,ratio_to_report(sal) over(partition by deptno) sal_ratio from scott.emp ;
[i]--[/i][i]查询成绩不及格的所有学生信息(提示:没有对应的表,只是意思意思。不及格人数大于等于三才能查)[/i]
select * from scott.emp where empno in(select distinct empno from scott.emp where 3<(select count(sal) from scott.emp where sal<3000) and empno in(select empno from scott.emp where sal<3000));
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image052.jpg[/img]
[i]--[/i][i]查询每个部门的平均工资[/i]
select distinct deptno,avg(sal) from scott.emp group by deptno order by deptno desc;
[i]--union[/i][i]组合查出的结果,但要求查出来的数据类型必须相同[/i]
select sal from scott.emp where sal >=all(select sal from scott.emp ) union select sal from scott.emp ;
select * from scott.emp a where a.empno between 7227 and 7369 ;[i]--[/i][i]只能从小到大[/i]
[i]---------[/i][i]创建表空间 要用拥有create tablespace权限的用户,比如sys[/i]
create tablespace tbs_dat datafile 'c:\oradata\tbs_dat.dbf' size 2000M;
[i]---------[/i][i]添加数据文件[/i]
alter tablespace tbs_dat add datafile 'c:\oradata\tbs_dat2.dbf' size 100M;
[i]---------[/i][i]改变数据文件大小[/i]
alter database datafile 'c:\oradata\tbs_dat.dbf' resize 250M;
[i]---------[/i][i]数据文件自动扩展大小[/i]
alter database datafile 'c:\oradata\tbs_dat.dbf' autoextend on next 1m maxsize 20m;
[i]---------[/i][i]修改表空间名称[/i]
alter tablespace tbs_dat rename to tbs_dat1;
[i]---------[/i][i]删除表空间 and datafiles 表示同时删除物理文件[/i]
drop tablespace tbs_dat including contents and datafiles;
[i]--substr(s1,s2,s3)[/i][i]:截取s1字符串,从s2开始,结束s3[/i]
select substr(job,3,length(job)) from scott.emp ;
[i]--replace[/i][i]:替换字符串[/i]
select replace(ename,'LL','aa') from scott.emp;
select * from scott.test;
insert into scott.test(ename,job) values('weather','好');
insert into scott.test(ename,job) values('wether','差');
[i]--soundex[/i][i]:返回一个与给定的字符串读音相同的字符串[/i]
select ename from scott.test where soundex(ename)=soundex('wether');
[i]--floor[/i][i]:取整数[/i]
select sal,floor(sal) as 整数 from scott.emp ;
[i]--log[/i][i](n,s):返回一个以n为低,s的对数[/i]
select empno,log(empno,2) as 对数 from scott.emp ;
[i]--mod(n1,n2)[/i][i]:返回一个n1除以n2的余数[/i]
select empno,mod(empno,2) as 余数 from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image053.jpg[/img]
[i]--power(n1,n2)[/i][i]:返回n1的n2次方根[/i]
select empno,power(empno,2) as 方根 from scott.emp ;
[i]--round[/i][i]和trunc:按照指定的精度进行舍入[/i]
select round(41.5),round(-41.8),trunc(41.6),trunc(-41.9) from scott.emp ;
[i]--sign[/i][i]:取数字n的符号,大于0返回1,小于0返回-1,等于0返回0[/i]
select sign(45),sign(-21),sign(0) from scott.emp ;
结果:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image054.jpg[/img]
select * from scott.emp;
oracle相关的数据库SQL查询语句:
1. 在职员表中查询出基本工资比平均基本工资高的职工编号。
2. 查询一个或者多个部门的所有员工信息,该部门的所有员工工资都高于公司的平均工资。
3. 现有张三的出生日期:1985-01-15 01:27:36,请各自新建表,将此日期时间插入表中,并计算出张三的年龄,显示张三的生日。
4. 生日的输出格式要求为MM-DD(未满两位的用0不全),张三的生日为01-15。
5. 算年龄要求用三个方式实现。
6. 生日要求用两个方式实现。
7. 在数据库表中有以下字符数据,如:
13-1,14-2,13-15,13-2,13-108,13-3,13-10,13-200,13-18,100-11,14-1
现在希望通过一条SQL语句进行排序,并且首先要按照前半部分的数字进行排序,然后再按照后半部分的数字进行排序,输出要拍成如下所示:
13-1,13-2,13-3,13-10,13-15,13-18,13-108,13-200,14-1,14-2,100-11
数据库表名:SellRecord;字段ListNumber;
8. 显示所有雇员的姓名以及满10年服务年限后的日期。
9. 显示雇员姓名,根据其服务年限,将最老的雇员排在最前面。
10显示所有雇员的姓名和加入公司的年份和月份,按雇员受雇日期所在月排序,将最早年份的职员排在最前面。
10. 显示假设一个月为30天的情况下所有雇员的日薪金。
11. 找出在(任何年份的)2月受聘的所有雇员(用两种方式实现)。
12. 对于每个雇员,显示其加入公司的天数。
13. 以年,月和日的方式显示所有雇员的服务年限(入职多少年/入职了多少月/入职了多少天)。
14. 找出各月最后一天受雇的所有雇员。
15. 找出早于25年之前受雇的雇员(用两种方式实现)。
16. 工资最低1500的职员增加10%,1500以上的增加5%的工资,用一条update语句实现(用两种方式实现)。
17. 按照部门统计每种岗位的平均工资,要求输出的格式如下图所示:
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image055.jpg[/img]
18.
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image056.jpg[/img]
19.
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image057.jpg[/img]
20.
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image058.jpg[/img]
21,。
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image059.jpg[/img]
22.
[img]http://files.jb51.net/file_images/article/201308/oraclesql/image060.jpg[/img]
本人声明:以上内容出现任何错误与不足,皆与本人无关。