使用基本查询语句
–(1)查询DEPT表显示所有部门名称.
select dname from dept;
–(2)查询EMP表显示所有雇员名及其全年收入(月收入=工资+补助),处理NULL行,并指定列别名为"年收入"。(NVL(comm,0) comm取空值时用0替代)
select e.ename,(e.sal+nvl(null,0))*12 年收入 from emp e ;
–(3)查询显示不存在雇员的所有部门号。
select d.deptno from emp e,dept d where d.deptno=e.deptno(+) group by d.deptno having count(e.deptno)=0;
select d.deptno from emp e right join dept d on d.deptno=e.deptno group by d.deptno having count(e.deptno)=0;
限制查询数据
–(1)查询EMP表显示工资超过2850的雇员姓名和工资。
select e.ename,e.sal from emp e where e.sal>2850 ;
–(2)查询EMP表显示工资不在1500~2850之间的所有雇员及工资。
select e.*,e.ename ,e.sal from emp e where e.sal<1500 or e.sal>2850;
–(3)查询EMP表显示代码为7566的雇员姓名及所在部门代码。
select e.ename,e.deptno from emp e where e.empno=7566;
select e.ename,d.deptno from emp e,dept d where e.deptno=d.deptno(+) and e.empno=7566;
select e.ename,d.deptno from emp e join dept d on d.deptno=e.deptno and e.empno=7566;
–(4)查询EMP表显示部门10和30中工资超过1500的雇员名及工资。
select e.ename,e.sal from emp e where (e.deptno=10 or e.deptno=30) and e.sal>1500;
select e.ename,e.sal from emp e where e.deptno in(10,30) and e.sal>1500;
–(5)查询EMP表显示第2个字符为"A"的所有雇员名其工资。
select e.ename,e.sal from emp e where e.ename like '_A%';
–(6)查询EMP表显示补助非空的所有雇员名及其补助。
select e.ename,e.comm from emp e where e.comm is not null;
排序数据
– (1)查询EMP表显示所有雇员名、工资、雇佣日期,并以雇员名的升序进行排序。
select e.ename,e.sal,e.hiredate from emp e order by e.ename;
– (2)查询EMP表显示在1981年2月1日到1981年5月1日之间雇佣的雇员名、岗位及雇佣日期,并以雇佣日期进行排序。
select e.ename,e.job,e.hiredate from emp e where e.hiredate>='1-2月-81' and e.hiredate<='1-5月-81' order by e.hiredate desc;
select e.ename,e.job,e.hiredate from emp e where e.hiredate between to_date('19810201','yyyy-mm-dd') and to_date('19810501','yyyy-mm-dd')
order by e.hiredate;
–(3)查询EMP表显示获得补助的所有雇员名、工资及补助,并以工资升序和补助降序排序。
select e.ename,e.sal,e.comm from emp e where e.comm is not null order by e.sal,e.comm desc;
2
–1.列出至少有一个雇员的所有部门。
select d.deptno from emp e right join dept d on d.deptno=e.deptno group by d.deptno having count(e.deptno) >0;
–2.列出薪金比“SMITH”多的所有雇员。
select e.ename from emp e where e.sal>(select max(sal) from emp where ename='SMITH');
–3.列出所有雇员的姓名及其上级的姓名。–注意没有上司的雇员
select e.ename,p.ename from emp e ,emp p where e.mgr = p.empno(+);
–4.列出入职日期早于其直接上级的所有雇员。
select e.ename,e.hiredate,p.ename,p.hiredate from emp e ,emp p where e.mgr = p.empno(+) and e.hiredate < p.hiredate ;
–5.列出部门名称和这些部门的雇员,同时列出那些没有雇员的部门。
select d.deptno,d.dname,e.ename from emp e right join dept d on d.deptno=e.deptno;
select e.ename ,d.deptno,d.dname from emp e full join dept d on e.deptno=d.deptno;
–6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select e.ename,d.dname from emp e ,dept d where e.deptno = d.deptno and e.job = 'CLERK';
select e.ename,d.dname,d.deptno from emp e join dept d on e.job='CLERK' and e.deptno=d.deptno;
–7.列出各种岗位的最低薪金,并显示最低薪金大于1500所有工作岗位及其最低薪资。
select e.job,min(e.sal) from emp e group by e.job having min(e.sal)>1500;
–8.列出从事“SALES”(销售)工作的雇员的姓名,假定不知道销售部的部门编号。
select e.ename ,d.dname from emp e ,dept d where e.deptno=d.deptno and d.dname='SALES';
select e.ename,d.dname,d.loc from emp e natural join dept d where d.dname='SALES';
–9.列出薪金高于公司平均的所有雇员。
select e.*,e.ename from emp e where (e.sal+nvl(e.comm,0))>(select avg(sal) from emp);
–10.列出与“SCOTT”从事相同工作的所有雇员。(…)
select e.job,e.ename from emp e where e.job in(select job from emp where ename='SCOTT' ) and e.ename<>'SCOTT' ;
–11.列出薪金等于在部门30工作的所有雇员的薪金的雇员的姓名和薪金。
select a.*, a.ename,a.sal from emp a where a.sal in (select e.sal from emp e where e.deptno=30 ) and (a.deptno<>30 or a.deptno is null);
select e.ename,e.sal,e.deptno from emp e where exists (select 1 from emp p where e.sal = p.sal and p.deptno = 30) and (e.deptno<>30 or e.deptno is null);
–12.列出薪金高于在部门30工作的所有雇员的薪金的雇员的姓名和薪金。
select e.* from emp e where e.sal > all(select distinct sal from emp where deptno=30 ) ;
select e.* from emp e where e.sal>(select max(sal) from emp where emp.deptno=30);
–13.列出在每个部门工作的雇员的数量以及其他信息。
select d.deptno,count(e.empno) from emp e right outer join dept d on d.deptno=e.deptno group by d.deptno;
select d.deptno ,d.dname,count(e.deptno) from dept d left join emp e on d.deptno=e.deptno group by d.deptno,e.deptno,d.dname;
select d.deptno,d.dname, count(e.deptno),d.loc
from dept d left outer join emp e on e.deptno=d.deptno
group by d.deptno,d.dname,d.loc;
–14.列出所有雇员的雇员名称、部门名称和薪金。
select e.ename,d.dname,e.sal from dept d ,emp e where e.deptno = d.deptno(+);
–15.列出分配有雇员数量的所有部门的详细信息即使是分配有0个雇员。
select d.deptno,d.dname,d.loc,count(e.deptno) from dept d left join emp e on e.deptno=d.deptno group by d.deptno,d.dname,d.loc;
–16.列出各种类别工作的最低工资。
select job,min(sal) from emp group by emp.job;
–17.列出各个部门的MANAGER(经理)的最低薪金。
select e.deptno,min(e.sal) from emp e where job='MANAGER' group by e.deptno;
–19.列出按计算的字段排序的所有雇员的年薪。
select e.ename,(e.sal+nvl(e.comm,0))*12 年薪 from emp e order by 年薪;
3
–1.找出各月倒数第3天受雇的所有员工.
select e.* from emp e where e.hiredate=last_day(e.hiredate)-2;
–2.找出早于12年前受雇的员工.
select e.ename from emp e where extract (year from e.hiredate)
select ename from emp where months_between(sysdate,hiredate)>144;
select e.* from emp e where e.hiredate
–3.以首字母大写的方式显示所有员工的姓名.
select initcap(lower(e.ename)) from emp e;
–4.显示正好为5个字符的员工的姓名.
select e.ename from emp e where length(e.ename)=5;
–5.显示不带有"R"的员工的姓名
select e.ename from emp e where upper(e.ename) not like '%R%';
–6.显示所有员工姓名的前三个字符.
select substr(e.ename,0,3) from emp e;
–7.显示所有员工的姓名,用a替换所有"A"
select replace(e.ename,'A','a') from emp e;
–8.显示满10年服务年限的员工的姓名和受雇日期.:
select e.ename ,e.hiredate from emp e where 2013-extract(year from e.hiredate)>=32;
select e.ename,e.hiredate from emp e where months_between(sysdate,e.hiredate)>120;
select e.* from emp e where e.hiredate<=trunc(sysdate)-(INTERVAL '10' YEAR);
--trunc(sysdate)+(INTERVAL '1' YEAR),
–9.显示员工的详细资料,按姓名排序.
select * from emp order by ename;
–10.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面.
select e.ename,e.hiredate from emp e order by e.hiredate ;
–11.显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序.
select e.ename,e.job,e.sal from emp e order by e.job,e.sal;
–12.显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面.
select e.ename ,e.hiredate,extract(MONTH from e.hiredate) month from emp e order by month,e.hiredate;
–13.显示在一个月为30天的情况所有员工的日薪金,忽略余数
select trunc(e.sal/30,0) from emp e;
–14.找出在(任何年份的)2月受聘的所有员工。
select e.ename from emp e where extract(month from e.hiredate)=2;
select e.*, e.ename from emp e where to_char(trunc(e.hiredate,'month'),'MM')=2;
select e.*, e.ename from emp e where to_char(trunc(e.hiredate,'month'),'MM')='02';
select e.*, e.ename from emp e where to_char(e.hiredate,'MM')='02';
–15.对于每个员工,显示其加入公司的天数.
select extract(day from e.hiredate)+(2013-extract(year from e.hiredate))*365 from emp e;
select trunc(sysdate-hiredate,0) from emp;
select e.*,ceil(sysdate-e.hiredate) "入职天数" from emp e ;
–16.显示姓名字段的任何位置包含"A"的所有员工的姓名.
select e.ename from emp e where upper(e.ename) like '%A%';
–17.以年月日的方式显示所有员工的服务年限. (大概)
select to_char(to_date('0001-01-01','yyyy-mm-dd')+(sysdate-hiredate)-365-1,'yyyy-mm-dd') from emp;
4
–1.用一个查询语句,实现查询各个岗位的总工资和各个部门的总工资和所有雇员的总工资
select e.job,sum(e.sal) from emp e group by e.job
union all
select d.deptno||'',nvl(sum(e.sal),0) from emp e right outer join dept d on d.deptno=e.deptno group by d.deptno
union all
select '', sum(e.sal) from emp e
–2.用一个查询语句,实现查询各个岗位的总工资和所有雇员的总工资
select e.job,sum(e.sal) from emp e group by e.job
union all
select '',sum(e.sal) from emp e ;
–3. 用一个查询语句,实现查询各个部门的总工资和各个岗位的总工资
select e.deptno||'',sum(e.sal) from emp e group by e.deptno
union all
select e.job, sum(e.sal) from emp e group by e.job;
select d.deptno||'',nvl(sum(e.sal),0) from emp e right outer join dept d on d.deptno=e.deptno group by d.deptno
union all
select e.job ,sum(e.sal) from emp e group by e.job;
–4. 通过查询显示每个员工的姓名和工资等级
select e.ename,s.grade from emp e join salgrade s on e.sal>=s.losal and e.sal<=s.hisal;
select e.*,s.grade from emp e ,salgrade s where e.sal between s.losal and s.hisal;
–5.通过查询显示员工的姓名和部门名称,并通过部门的不同分组
select e.ename,d.deptno,d.dname from emp e join dept d on e.deptno=d.deptno order by d.deptno;
select e.ename,d.dname from emp e, dept d where e.deptno = d.deptno order by d.dname;
–6. 查询每个员工的姓名和他的上司的名字
select e.ename,p.ename from emp e inner join emp p on e.mgr=p.empno;
–7.查询按工资排序后6到10行的员工信息
select c.* from(select b.* ,rownum r from (select a.* from emp a order by a.sal desc) b ) c where c.r>=6 and c.r<=10;
--rowid
select rowid , emp.* from emp
–(1) 查询20号部门的所有员工信息。
select * from emp e where e.deptno=20;
–(2) 查询奖金(COMM)高于工资(SAL)的员工信息。
select * from emp where comm>sal;
–(3) 查询奖金高于工资的20%的员工信息。
select * from emp where comm>sal*0.2;
–(4) 查询10号部门中工种为MANAGER和20号部门中工种为CLERK的员工的信息。
select * from emp e
where (e.deptno=10 and e.job='MANAGER')
or (e.deptno=20 and e.job='CLERK')
–(5) 查询所有工种不是MANAGER和CLERK,
–且工资大于或等于2000的员工的详细信息。
select * from emp
where job not in('MANAGER','CLERK') and sal>=2000;
–(6) 查询有奖金的员工的不同工种。
select * from emp where comm is not null;
–(7) 查询所有员工工资和奖金的和。
select (e.sal+nvl(e.comm,0)) from emp e;
–(8) 查询没有奖金或奖金低于100的员工信息。
select * from emp where comm is null or comm<100;
–(9) 查询员工工龄大于或等于10年的员工信息。
select * from emp where (sysdate-hiredate)/365>=10;
–(10) 查询员工信息,要求以首字母大写的方式显示所有员工的姓名。
select initcap(ename) from emp;
select upper(substr(ename,1,1))||lower(substr(ename,2)) from emp;
–(11) 显示所有员工的姓名、入职的年份和月份,按入职日期所在的月份排序,
–若月份相同则按入职的年份排序。
select ename,to_char(hiredate,'yyyy') year,to_char(hiredate,'MM') month
from emp
order by month,year;
–(12) 查询在2月份入职的所有员工信息。
select * from emp where to_char(hiredate,'MM')='02'
–(13) 查询所有员工入职以来的工作期限,用“年月**日”的形式表示。
select e.ename,floor((sysdate-e.hiredate)/365)||'年'
||floor(mod((sysdate-e.hiredate),365)/30)||'月'
||floor(mod(mod((sysdate-e.hiredate),365),30))||'日'
from emp e;
–(14) 查询从事同一种工作但不属于同一部门的员工信息。
select a.ename,a.job,a.deptno,b.ename,b.job,b.deptno
from emp a,emp b
where a.job=b.job and a.deptno<>b.deptno;
–(15) 查询各个部门的详细信息以及部门人数、部门平均工资。
select d.deptno,count(e.empno),avg(e.sal),d.dname,d.loc
from emp e ,dept d
where e.deptno=d.deptno
group by d.deptno,d.dname,d.loc
–(16) 查询10号部门员工以及领导的信息。
select * from emp where empno in(
select mgr from emp where deptno=10) or deptno=10;
–(17) 查询工资为某个部门平均工资的员工信息。
select * from emp
where sal in(select avg(sal) from emp group by deptno);
–(18) 查询工资高于本部门平均工资的员工的信息。
select * from emp e1
where sal >(select avg(sal) from emp e2 where e2.deptno=e1.deptno);
–(19) 查询工资高于本部门平均工资的员工的信息及其部门的平均工资。
select e.*,a.avgsal
from emp e,
(select deptno,avg(sal) as avgsal from emp group by deptno) a
where a.deptno=e.deptno and e.sal>a.avgsal;
–(20) 统计各个工种的人数与平均工资。
select count(*),e.job,avg(e.sal) from emp e
group by e.job
–(21) 统计每个部门中各个工种的人数与平均工资。
select deptno,job,count(empno),avg(sal) from emp e
group by e.deptno,e.job
–(22) 查询所有员工工资都大于1000的部门的信息。
select * from dept where deptno in
(select deptno from emp
where deptno not in
(select distinct deptno from emp where sal<1000));
–(23) 查询所有员工工资都大于1000的部门的信息及其员工信息。
select * from emp e join dept d
on d.deptno
in (select deptno from emp
where deptno not in
(select distinct deptno from emp where sal<1000))
and d.deptno=e.deptno;
–(24) 查询所有员工工资都在900~3000之间的部门的信息。
select * from dept
where deptno not in(
select deptno from emp
where sal not between 900 and 3000);
–(25) 查询所有工资都在900~3000之间的员工所在部门的员工信息。
select * from emp a
where a.deptno in
(select distinct e.deptno from emp e
where e.sal between 900 and 3000);
–(26) 查询每个员工的领导所在部门的信息。
select d.* from dept d
where d.deptno in
(select distinct e2.deptno from emp e1,emp e2
where e1.empno=e2.mgr);
–(27) 查询人数最多的部门信息。
select * from dept
where deptno in
(select deptno from (select count(*) count,deptno from emp group by deptno)
where count in(select max(count)
from (select count(*) count ,deptno from emp group by deptno)));
–(28) 查询30号部门中工资排序前3名的员工信息。
select * from
(select sal from emp where deptno=30 order by sal desc) e
where rownum<4
–(29) 查询’JONES’员工及所有其直接、间接下属员工的信息。
select e.* from emp e
start with ename='JONES'
connect by prior empno=mgr;
—(30) 查询SCOTT员工及其直接、间接上级员工的信息。
select e.* from emp e
start with ename='SCOTT'
connect by prior mgr=empno;
–(31) 以树状结构查询所有员工与领导之间的层次关系。
select substr(sys_connect_by_path(ename,'->'),3),level
from emp
start with mgr is null
connect by prior empno=mgr;
–(32)将各部门员工的工资修改为该员工所在部门平均工资加1000。
update emp e set sal=1000+(select avg(sal) from emp where deptno=e.deptno);
– (33)查询工作等级为2级,1985年以后入职的工作地点为DALLAS的员工编号、
–姓名和工资。
select e.ename,e.empno,e.sal from emp e,salgrade s,dept d
where (e.sal between s.losal and s.hisal)
and (s.grade=2)
and to_char(e.hiredate,'yyyy')>1985
and e.deptno=d.deptno
and d.loc='DALLAS';
– (34)部门平均薪水最高的部门编号
select * from(
select avg(sal) avgsal,deptno
from emp group by deptno order by avgsal desc)
where rownum=1;
select deptno,avg(sal) from emp group by deptno having avg(sal)=(
select max(avg(sal)) avgsal
from emp group by deptno)
– (35)部门平均薪水最高的部门名称
select d.* from dept d where deptno in(
select deptno from emp group by deptno having avg(sal)=(
select max(avg(sal)) avgsal
from emp group by deptno))
– (36)平均薪水最低的部门的部门名称
select d.* from dept d where deptno in(
select deptno from emp group by deptno having avg(sal)=(
select min(avg(sal)) avgsal
from emp group by deptno))
– (37)平均薪水等级最低的部门的部门名称
select d.dname from dept d
where d.deptno in
(select a.deptno from
(select e.deptno from emp e,salgrade s
where (e.sal between s.losal and s.hisal)
group by e.deptno order by avg(s.grade)) a
where rownum=1);
– (38)部门经理人中,薪水最低的部门名称
select dname from dept where deptno=
(select deptno from
(select deptno from emp where job='MANAGER' group by deptno
order by min(sal)) where rownum=1)
– (39)比普通员工的最高薪水还要高的经理人名称
select ename from emp where sal>
(select max(sal) from emp where job not in
('MANAGER','PRESIDENT')) and job='MANAGER' or job='PRESIDENT'
– (40)删除重复部门,但是留下一项
insert into dept values(70,'RESEARCH','DALLAS')
select deptno,dname,rowid from dept
delete from dept d
where rowid<>
(select min(rowid) from dept where dname=d.dname and d.loc=loc)
– (41)更新员工工资为他的主管的工资,奖金
update emp e set sal=(select sal from emp where empno=e.mgr),
comm=(select comm from emp where empno=e.mgr)
update emp e set (sal,comm)=(select sal,comm from emp where empno=e.mgr)
rollback;
select * from emp;