–用SQL完成以下问题列表:
–1.列出至少有一个员工的所有部门。
select * from dept d where
exists(select * from emp where deptno =d.deptno);
select distinct d.* from dept d,emp e
where d.deptno=e.deptno;
–2.列出薪金比“SMITH”多的所有员工。
select * from emp where sal >(
select sal from emp where ename=’SMITH’);
–3.列出所有员工的姓名及其直接上级的姓名。
select ename,(select ename from emp where empno =e.mgr) from emp e ;
select e.ename,m.ename from emp e,emp m
where e.mgr=m.empno;
–4.列出受雇日期早于其直接上级的所有员工。
select * from emp e,emp m
where e.mgr=m.empno
and e.hiredate<m.hiredate;
select * from emp e where hiredate < (
select hiredate from emp where empno =e.mgr
);
–5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
select * from dept d,emp e
where d.deptno=e.deptno(+);
–6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select ename, (select dname from dept where deptno = e.deptno)
from emp e
where job = ‘CLERK’;
select e.ename,d.dname from emp e , dept d
where e.deptno =d.deptno
and e.job=’CLERK’;
–7.列出最低薪金大于1500的各种工作。
select min(sal),job from emp group by job having min(sal) >1500;
–8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select * from emp where deptno =
(select deptno from dept where dname=’SALES’);
select * from emp e, dept d
where e.deptno=d.deptno
and d.dname=’SALES’;
–9.列出薪金高于公司平均薪金的所有员工。
select * from emp where sal > (select avg(sal) from emp);
–10.列出与“SCOTT”从事相同工作的所有员工。
select * from emp where job =(
select job from emp where ename =’SCOTT’) and ename!=’SCOTT’;
–11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
select *from emp where sal in(
select sal from emp where deptno = 30);
select *from emp where sal =any(
select sal from emp where deptno = 30);
–12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
select *from emp where sal >all(
select sal from emp where deptno = 30);
select *from emp where sal >(
select max(sal) from emp where deptno = 30);
–13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select count(1),avg(sal), avg(sysdate-e.hiredate)/365 from emp e, dept d
where e.deptno=d.deptno
group by d.deptno;
–14.列出所有员工的姓名、部门名称和工资。
select ename,
(select dname from dept wher e deptno = e.deptno),
sal + nvl(comm, 0)
from emp e;
–15.列出所有部门的详细信息和部门人数。
select d.*,(select count(1) from emp where deptno =d.deptno) from dept d;
–16.列出各种工作的最低工资。
select min(sal+nvl(comm,0)),job from emp group by job;
–17.列出各个部门的MANAGER(经理)的最低薪金。
select
min(sal),deptno
from emp where job=’MANAGER’
group by deptno;
–18.列出所有员工的年工资,按年薪从低到高排序。
select (sal+nvl(comm,0))*12 from emp order by (sal+nvl(comm,0))*12;
–1.找出EMP表中的姓名(ENAME)第三个字母是A 的员工姓名。
select * from emp where ename like ‘__A%’;
–2.找出EMP表员工名字中含有A 和N的员工姓名。
select * from emp where ename like ‘%A%’ and ename like ‘%N%’;
–3.找出所有有佣金的员工,列出姓名、工资、佣金,
–显示结果按工资从小到大,佣金从大到小。
select ename, sal + nvl(comm, 0), comm
from emp
where (comm is not null and comm >0)
order by sal + nvl(comm, 0), comm desc;
–4.列出部门编号为20的所有职位。
select job from emp where deptno =20;
–5.列出不属于SALES 的部门。
select * from emp e, dept d
where e.deptno=d.deptno
and d.dname !=’SALES’;
–6.显示工资不在1000 到1500 之间的员工信息:名字、工资,按工资从大到小排序。
select ename, sal + nvl(comm, 0)
from emp
where sal + nvl(comm, 0) not between 1000 and 1500
order by sal + nvl(comm, 0) desc;
–7.显示职位为MANAGER 和SALESMAN,
–年薪在15000 和20000 之间的员工的信息:名字、职位、年薪。
select ename, job, (sal + nvl(comm, 0)) * 12
from emp
where job in (‘MANAGER’, ‘SALESMAN’)
and (sal + nvl(comm, 0)) * 12 between 15000 and 20000;
原创文章,作者:Carrie001128,如若转载,请注明出处:https://blog.ytso.com/244871.html