oracle查询16个小题

--1、查询出至少有一个雇员的所有部门
select Dept_id from emp group by Dept_id having count(*)>0;

select dname from dept where Dept_id in(select Dept_id from emp);
--2、查询出薪金比输入的一个雇员多的所有雇员
select ename,(sal+comm) from emp where (sal+comm)>(select (sal+comm) from emp where ename='张三');

--3、输入一个部门名称查询该部门中所有雇员的姓名及其直接上级的姓名
select e.empno,e.ename, e.mgr,e2.ename
from scott.emp e
left join scott.emp e2 on e.mgr=e2.empno
where e.deptno=(select deptno from scott.dept where dname='ACCOUNTING');

--4、查询出入职日期早于其直接上级的所有雇员
select e.empno,e.ename,e.hiredate,e.mgr,e2.ename,e2.hiredate
from scott.emp e
left join scott.emp e2 on e.mgr=e2.empno
where e.hiredate < e2.hiredate;
--5、查询出每个部门名称和部门中雇员人数(部门中没有雇员也要显示 人数为0)
SELECT DEPT.*,EMP.* FROM DEPT LEFT JOIN EMP ON DEPT.D_NO=EMP.D_NO;
select dept.*,emp.* from dept left join emp on dept.dept_id=emp.dept_id;

--6、查询出所有软件开发工程师的姓名及其部门名称
select e.ename,e.job,d.dept_id
from emp e,dept d
where e.dept_id=d.dept_id and job='测试工程师';

--7、查询出各种职位的最低薪金,排除最低薪金大于15000的记录
select min(sal),job from emp group by job having min(sal)>4000;
--8、查询出部门名称为"软件开发87部"的雇员的姓名
select * from emp where dept_id=
(select dept_id from dept where dname='测试部');
--9、查询出薪金高于公司平均水平的所有雇员
select ename,emp_id from emp
where sal>(select avg(sal) from emp);
--10、查询出与某一位雇员从事相同职位的所有雇员
select emp_id,ename from emp where job=
(select job from emp where ename='测试部');

--11、查询出各种职位的最低工资
select min(sal),job from emp group by job;
--12、查询出各个部门的项目经理的最低薪金
select mgr,min(sal) from emp group by mgr;
--13、查询出按年薪排序的所有雇员的年薪
select sal from emp order by sal*12 desc;
--14、查询出所有雇员的姓名的前二个字符
select substr(ename,1,2) from emp;
--15、查询出所有雇员的姓名以及满10年服务年限的日期
select ename,emp_id,hiredate,add_months(hiredate,120) from emp;
--16、查询出是每年2月来的所有雇员
select ename ,empno ,hiredate from scott.emp where
extract(month from hiredate)=2;

本文仅代表作者观点,系作者@请多指教 发表。
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
http://www.cnblogs.com/yushengaqingzhijiao/p/8305648.html
原文地址:https://www.cnblogs.com/yushengaqingzhijiao/p/8305648.html