测试题

测试题_第1张图片

--1.查询属于“ACCOUNTING”部门下所有的员工的员工号,姓名,并按姓名升序排列
--法一:子查询(select嵌套)
select empno,ename from emp  
where deptno=(select deptno from dept where dname='ACCOUNTING') 
order by ename;  
--法二:多表查询
select e.empno,e.ename from emp e,dept d
where e.deptno=d.deptno and d.dname='ACCOUNTING';

--2.alter table emp01 add Eremark varchar2(40);--删除字段的时候才会出现column
alter table emp01 drop column Eremark;

--3.update emp01 
set sal=sal+1000
where sal>=2000 and sal<=3000;

--4.select ename,job,sal,deptno from emp
where job in (select distinct job from emp where deptno=10);--distinct的作用是去重,in后面跟的是集合

--5.
方法一:
--先得到部门平均工资:
select deptno,avg(sal) from emp group by deptno;
--查询高于自己部门平均工资的员工的信息
select * from emp where sal>
(select avg(sal) from emp group by deptno);--此处子查询返回值要保证唯一,如果前面是in则不需要保证唯一性
--不理解
select * from emp e1 where e1.sal>
(select avg(sal) from emp e2 where e2.deptno=e1.deptno group by e2.deptno);
方法二:
(1)查询各部门平均工资
select deptno,avg(sal) avg_sal from emp group by deptno;--x
(2)将x表和emp表做表连接
select e.*,x.* from emp e,x
where e.deptno=x.deptno and e.sal>x.avg_sal;
(3)替换x
select e.*,x.* from emp e,(select deptno,avg(sal) avg_sal from emp group by deptno)x
where e.deptno=x.deptno and e.sal>x.avg_sal;

--6.自连接
--分析:员工表,领导表
select w.ename,m.ename from emp w,emp m
where w.mgr=m.empno and w.hiredate

你可能感兴趣的:(Oracle)