oracle数据库常用函数 语法

–(1)查询20号部门的所有员工信息。
select * from emp e where e.deptno=20

–(2)查询所有工种为CLERK的员工的工号、员工名和部门名。
select e.empno,e.ename,d.dname from emp e natural join dept d where e.job=‘CLERK’

–(3)查询奖金(COMM)高于工资(SAL)的员工信息。
select * from emp e where e.sal<e.comm

–(4)查询奖金高于工资的20%的员工信息。
select * from emp e where e.sal*0.2<e.comm

–(5)查询10号部门中工种为MANAGER和20号部门中工种为CLERK的员工的信息。
select * from emp e where (e.deptno=10 and e.job=‘MANAGER’) or (e.deptno=20 and e.job=‘CLERK’)

–(6)查询所有工种不是MANAGER和CLERK,且工资大于或等于2000的员工的详细信息。
select * from emp e where e.job !=‘MANAGER’ and e.job !=‘CLERK’ and e.sal>2000

–(7)查询有奖金的员工的不同工种。
select distinct e.job from emp e where e.comm is not null

–(8)查询所有员工工资和奖金的和。
select sum(e.sal+nvl(e.comm,0)) from emp e

–(9)查询没有奖金或奖金低于100的员工信息。
select * from emp e where e.comm is null or e.comm<100

–(10)查询各月倒数第2天入职的员工信息。
select * from emp e where e.hiredate=(last_day(e.hiredate)-1)

–(11)查询员工工龄大于或等于10年的员工信息。
select * from emp e where months_between(sysdate,e.hiredate)/12>=10

–(12)查询员工信息,要求以首字母大写的方式显示所有员工的姓名。
select initcap(e.ename) from emp e

–(13)查询员工名正好为6个字符的员工的信息。
select * from emp e where length(e.ename)=6

–(14)查询员工名字中不包含字母“S”员工。
select * from emp e where instr(e.ename,‘S’)=0

–(15)查询员工姓名的第2个字母为“M”的员工信息。
select * from emp e where e.ename like ‘_M%’

–(16)查询所有员工姓名的前3个字符。
select substr(e.ename,1,3) from emp e;

–(17)查询所有员工的姓名,如果包含字母“s”,则用“S”替换。
select replace(lower(e.ename),‘s’,‘S’) from emp e;

–(18)查询员工的姓名和入职日期,并按入职日期从先到后进行排列。
select e.ename,e.hiredate from emp e order by e.hiredate

–(19)显示所有的姓名、工种、工资和奖金,按工种降序排列,若工种相同则按工资升序排列。
select e.ename,e.job,e.sal,e.comm from emp e order by e.job desc,e.sal asc

–(20)显示所有员工的姓名、入职的年份和月份,若入职日期所在的月份排序,若月份相同则按入职的年份排序。
select e.ename,extract(year from e.hiredate) y,extract(month from e.hiredate) m from emp e order by m,y

–(21)查询在2月份入职的所有员工信息。
select * from emp e where extract(month from e.hiredate)=2

–(22)查询所有员工入职以来的工作期限,用“月**日”的形式表示。

select extract(year from e.hiredate)||‘年’||extract(month from e.hiredate)||‘月’||extract(day from e.hiredate)||‘日’ from emp e

–(23)查询至少有一个员工的部门信息。
select distinct d.* from dept d join emp e on d.deptno=e.deptno

–(24)查询工资比SMITH员工工资高的所有员工信息。
select * from emp e where e.sal>(select e.sal from emp e where e.ename=‘SMITH’)

–(25)查询所有员工的姓名及其直接上级的姓名。
select m.ename,e.ename from emp e join emp m on e.empno=m.mgr;

–(26)查询入职日期早于其直接上级领导的所有员工信息。
select m.* from emp e join emp m on e.empno=m.mgr and e.hiredate<m.hiredate;

–(27)查询所有部门及其员工信息,包括那些没有员工的部门。
select * from emp e right join dept d on e.deptno=d.deptno

–(28)查询所有员工及其部门信息,包括那些还不属于任何部门的员工。
select * from emp e left join dept d on e.deptno=d.deptno

–(29)查询所有工种为CLERK的员工的姓名及其部门名称。
select e.ename,d.dname from emp e natural join dept d where e.job=‘CLERK’

–(30)查询最低工资大于2500的各种工作。
select distinct e.job from emp e where e.sal > 2500

–(31)查询最低工资低于2000的部门及其员工信息。
select *
from emp
where deptno in
(select deptno
from (select min(sal) min_sal, deptno from emp group by deptno)
where min_sal < ‘2000’);

–(32)查询在SALES部门工作的员工的姓名信息。
select e.ename from emp e natural join dept d where d.dname=‘SALES’;

–(33)查询工资高于公司平均工资的所有员工信息。
select * from emp e where e.sal > (select avg(e.sal) from emp e)

–(34)查询与SMITH员工从事相同工作的所有员工信息。
select * from emp e where e.job=(select e.job from emp e where e.ename=‘SMITH’)

–(35)列出工资等于30号部门中某个员工工资的所有员工的姓名和工资。
select e.ename,e.sal from emp e where e.sal = any(select e.sal from emp e where e.deptno=30)

–(36)查询工资高于30号部门中工作的所有员工的工资的员工姓名和工资。

select e.ename,e.sal from emp e where e.sal > all(select e.sal from emp e where e.deptno=30)

–(37)查询每个部门中的员工数量、平均工资和平均工作年限。
select count(1),avg(e.sal),avg(months_between(sysdate,e.hiredate)/12) from emp e group by e.deptno

–(38)查询从事同一种工作但不属于同一部门的员工信息。
select e.* from emp e join emp m on e.job=m.job and e.deptno!=m.deptno

–(39)查询各个部门的详细信息以及部门人数、部门平均工资。
select *
from (select d.deptno deptno, count(e.deptno), avg(e.sal)
from emp e
right join dept d
on e.deptno = d.deptno
group by d.deptno) m natural
join dept d

–(40)查询各种工作的最低工资。
select e.job, min(e.sal) from emp e group by e.job

–(41)查询各个部门中的不同工种的最高工资。
select e.deptno,e.job,max(e.sal) from emp e group by e.deptno,e.job

–(42)查询10号部门员工以及领导的信息。
select * from emp e where e.deptno=10;

–(43)查询各个部门的人数及平均工资。
select e.deptno, count(1) , avg(e.sal) from emp e group by e.deptno

–(44)查询工资为某个部门平均工资的员工信息。
select * from emp e where e.sal in (select avg(e.sal) from emp e group by e.deptno)

–(45)查询工资高于本部门平均工资的员工的信息。
select e.*
from emp e natural
join (select e.deptno d, avg(e.sal) a from emp e group by e.deptno) m
where e.sal > m.a
and e.deptno = m.d

–(46)查询工资高于本部门平均工资的员工的信息及其部门的平均工资。
select e.*,m.a
from emp e natural
join (select e.deptno d, avg(e.sal) a from emp e group by e.deptno) m
where e.sal > m.a
and e.deptno = m.d

–(47)查询工资高于20号部门某个员工工资的员工的信息。
select *
from emp e
where e.sal > any (select e.sal from emp e where e.deptno = 20)

–(48)统计各个工种的人数与平均工资。
select e.job,count(e.empno) ,nvl(avg(e.sal),0) from emp e group by e.job

–(49)统计每个部门中各个工种的人数与平均工资。
select e.deptno, e.job, count(e.empno), nvl(avg(e.sal), 0)
from emp e
group by e.job, e.deptno
order by e.deptno

–(50)查询工资、奖金与10 号部门某个员工工资、奖金都相同的员工的信息。
select *
from emp e
where e.sal in (select e.sal from emp e where e.deptno = 10)
and e.comm in (select e.comm from emp e where e.deptno = 10)

select emp.*
from emp
join (select sal, comm from emp where deptno = 10) t
on emp.sal = t.sal
and nvl(emp.comm, 0) = nvl(t.comm, 0)
and emp.deptno != 10;

–(51)查询部门人数大于5的部门的员工的信息。
select e.*
from emp e natural
join (select e.deptno d, count(1) a from emp e group by e.deptno) m
where m.a > 5 and e.deptno=m.d

–(52)查询所有员工工资都大于1000的部门的信息。
select *
from dept d
where d.deptno not in (select e.deptno from emp e where e.sal < 1000)

–(53)查询所有员工工资都大于1000的部门的信息及其员工信息。

select distinct *
from emp e
join (select *
from dept d
where d.deptno not in
(select e.deptno from emp e where e.sal < 1000)) m
on e.deptno = m.deptno

–(54)查询所有员工工资都在900~3000之间的部门的信息。
–一
select *
from dept d
where d.deptno in
(select distinct d.deptno
from dept d
right join emp e
on e.deptno = d.deptno
and d.deptno not in
(select e.deptno
from emp e
where e.sal not between 900 and 3000))
–二
select *
from dept
where deptno in (select distinct deptno
from emp
where deptno not in
(select distinct deptno
from emp
where sal not between 900 and 3000));

–(55)查询所有工资都在900~3000之间的员工所在部门的员工信息。
select *
from emp e
where e.deptno not in
(select e.deptno from emp e where e.sal not between 900 and 3000)

–(56)查询每个员工的领导所在部门的信息。
select *
from dept d
where d.deptno in
(select m.deptno from emp e right join emp m on e.mgr = m.empno)

–(57)查询人数最多的部门信息。
select *
from dept d
join (select e.deptno d, count(1) a from emp e group by e.deptno) m
on d.deptno = m.d
and m.a = (select max(count(1)) a from emp e group by e.deptno)

–(58)查询30号部门中工资排序前3名的员工信息。
select *
from (select * from emp e where e.deptno = 30 order by e.sal desc) m
where rownum <= 3

–(59)查询所有员工中工资排在5~10名之间的员工信息。
select *
from (select m.*, rownum r
from (select * from emp e order by e.sal desc) m
where rownum <= 10) mm
where mm.r > 5

–(60)向emp表中插入一条记录,员工号为1357,员工名字为oracle,工资为2050元,部门号为20,入职日期为2002年5月10日。
insert into emp (empno,ename,hiredate,sal,deptno) values(1357,‘oracle’,‘5/10月/2002’,2050,20);

select * from emp

–(61)向emp表中插入一条记录,员工名字为FAN,员工号为8000,其他信息与SMITH员工的信息相同。
insert into emp values(8000,‘FAN’,(select job,mgr,hiredate,sal,comm,deptno from emp where ename=‘SMITH’))

–(62)将各部门员工的工资修改为该员工所在部门平均工资加1000。
update emp t1
set sal =
(select new_sal
from (select avg(sal) + 1000 new_sal, deptno
from emp
group by deptno) t2 wher e t1.deptno = t2.deptno);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值