java面试之oracle数据库相关试题以及答案

--create table dept
--(
--deptno varchar(10) primary key,
--dname varchar(10)
--);
--create table emp
--(
--empno varchar(10) primary key,
--ename varchar(10),
--job varchar(10),
--mgr varchar(10),
--sal varchar(10),
--deptno varchar(10) references dept(deptno)
--);
--drop table dept;
--drop table emp;
--insert into dept values ('1','事业部');
--insert into dept values ('2','销售部');
--insert into dept values ('3','技术部');

--insert into emp values ('01','jacky','clerk','tom','1000','1');
--insert into emp values ('02','tom','clerk','','2000','1');
--insert into emp values ('07','biddy','clerk','','2000','1');
--insert into emp values ('03','jenny','sales','pretty','600','2');
--insert into emp values ('04','pretty','sales','','800','2');
--insert into emp values ('05','buddy','jishu','canndy','1000','3');
--insert into emp values ('06','canndy','jishu','','1500','3');

select * from dept;
select * from emp;

--1列出emp表中各部门的部门号,最高工资,最低工资
select deptno as 部门编号,max(sal) as 最高工资,min(sal)as 最低工资 From emp group by deptno;

--2 列出emp表中各部门job为'CLERK'的员工的最低工资,最高工资
select deptno as 部门编号 ,max(sal) as 最高工资,min(sal)as 最低工资 from emp where  job = 'clerk' group by deptno;

--3 对于emp中最低工资小于2000的部门,列出job为'CLERK'的员工的部门号,最低工资,最高工资
select t.deptno as 部门编号 ,max(t.sal) as 最高工资,min(t.sal)as 最低工资 from emp t where t.job='clerk' and (select min(sal) from emp t1 where t1.deptno = t.deptno)<2000 group by t.deptno;

--4 根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资
select * from emp order by deptno desc,sal asc;

--5 列出'buddy'所在部门中每个员工的姓名与部门号
select * from emp where deptno = (select deptno from emp where ename='buddy');

--6 列出每个员工的姓名,工作,部门号,部门名
select e.ename,e.job,d.dname from emp e left join dept d on e.deptno = d.deptno;

--7列出emp中工作为'CLERK'的员工的姓名,工作,部门号,部门名
select e.ename,e.job,d.dname from emp e left join dept d on e.deptno = d.deptno where job = 'clerk';

--8对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)
select ename,mgr from emp where mgr is not null;

select e2.ename,e2.mgr from emp e1 join emp e2 on e1.ename = e2.mgr

--9 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作
select * from dept d left join (select * from emp where job = 'clerk') e on e.deptno = d.deptno ;

--10 对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
select * from emp e where sal > (select avg(e1.sal) from emp e1 where e1.deptno = e.deptno  ) order by e.deptno;

--11对于emp,列出各个部门中工资高于本部门平均工资的员工数和部门号,按部门号排序
select * from emp e where sal > (select avg(e1.sal) from emp e1 where e1.deptno = e.deptno  ) order by e.deptno;

--12对于emp中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,平均工资,按部门号排序
select e.deptno,count(e.deptno),avg(sal) from emp e where sal > (select avg(e1.sal) from emp e1 where e1.deptno = e.deptno  ) group by e.deptno having count(e.deptno)>1 order by e.deptno;

--13对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
select e.deptno,e.ename,e.sal,(select count(*) from emp where sal<e.sal) as sum from emp e where  (select count(*) from emp where sal<e.sal )>=5

你可能感兴趣的:(java,oracle,面试)