其实写博客的这个想法我有了很久了,但一直觉得自己文采不行,怕是不能将一些问题讲述清楚。后来想到写了也不一定就有人看得到,权且就当做是自己的一个笔记吧。
今天要发布的是一个oracle的sql查询例子,里面的sql语句都是博主自己写的,虽然可能不是最好的答案,但也都能查询成功。这个例子也如标题所讲的把sql的一些分组查询、子查询等等都运用上了。我们就废话不多说,看例子吧。
因为博主用到的是scott用户下的表,所以查询的时候加了scott,各位朋友注意一下就行了。
这个例子总共用到三个表。
emp表(员工信息表)
dept表(部门信息表)
salgrade表(工资等级表)
–1:列出所有员工的姓名,部门名称,和工资
select ename,job,sal from scott.emp;
–2:列出所有部门的详细信息和部门人数
select * from scott.emp;
select * from scott.dept;
select e.deptno, d.dname,count(e.deptno) as xx from scott.emp e,scott.dept d where e.deptno = d.deptno(+) group by d.dname ,e.deptno;
–3:列出所有员工的年工资,所在部门名称,按年薪升序排列
select e.ename,e.sal*12,d.dname from scott.emp e,scott.dept d where e.deptno = d.deptno(+) order by e.sal*12 asc;
–4:查出每个员工的上级主管及所在部门名称,并要求这些主管的薪水超过3000
select e1.ename,e1.mgr,d.dname,e1.sal from scott.emp e1,scott.emp e2,dept d
where e1.deptno = d.deptno(+)
and e1.empno = e2.mgr
and e1.sal>3000;
select e.ename,d.dname,e.mgr
from (select e1.sal ,e2.mgr from scott.emp e1,scott.emp e2
where e1.deptno=e2.deptno
and e2.mgr=e1.empno
and e1.sal>3000) t1,scott.emp e,scott.dept d
where e.deptno=d.deptno
and e.mgr=t1.mgr;
–5:求出部门名称中带’S’字符的部门员工的工资合计,部门人数
select d.dname,sum(e.sal),count(e.deptno)
from scott.emp e,scott.dept d
where e.deptno=d.deptno
and d.dname like'%S%'
group by d.dname;
–6:列出部门名称和这些部门的员工信息(数量,平均工资),同时列出那些没有员工的部门
select d.dname,count(e.deptno) as 人数,avg(e.sal)
from scott.emp e,scott.dept d
where e.deptno(+)=d.deptno
group by d.dname;
–7:列出在部门”SALES”工作的员工姓名,基本工资,雇用日期,部门名称,假定不知道销售部的部门编号
select e.ename,e.sal,e.hiredate,d.dname
from scott.emp e,scott.dept d
where e.deptno=d.deptno
and d.dname='SALES';
–8:列出公司各个工资等级雇员的数量,平均工资
select * from scott.salgrade;
select s.grade,count(e.sal),avg(e.sal)
from scott.emp e,scott.salgrade s
where e.sal>=s.losal
and e.sal<=s.hisal
group by s.grade;
–9:列出薪水高于在部门30工作的所有员工的薪金的员工姓名和薪金,部门名称
select e.ename,e.sal,d.dname
from scott.emp e,scott.dept d,(select max(sal) msal from scott.emp where deptno=30) t1
where e.deptno=d.deptno
and e.sal>t1.msal;
–10:列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称,部门位置,部门人数
select e.empno,e.ename,d.dname,d.loc,count(e.deptno)
from scott.emp e,scott.dept d,(select mgr ,hiredate from scott.emp) t1
where e.deptno=d.deptno
and e.mgr=t1.mgr
and to_char(e.hiredate)<to_char(t1.hiredate);
–11:列出所有“clerk”的姓名及其部门名称,部门人数,工资等级
select e.ename,d.dname,count(e.deptno),s.grade
from scott.emp e,scott.dept d,scott.salgrade s
where e.deptno=d.deptno
and e.sal>=s.losal
and e.sal<=s.hisal
and job='CLERK'
group by e.ename,d.dname,s.grade;
–12:列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数及所在部门名称,位置,平均工资
select t1.job,count(t1.job),d.dname,d.loc,avg(t1.sal)
from scott.dept d,(select e.job,e.sal,e.deptno
from scott.emp e
where e.sal>1500) t1
where t1.deptno=d.deptno
group by t1.job,d.dname,d.loc;
–13:列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,公司的工资等级
select avg(e.sal) as xx from emp e;
select * from emp;
select e.ename,d.dname,e.mgr,s.grade
from emp e,dept d,salgrade s,
(select avg(e.sal) as xx
from emp e) a
where e.deptno = d.deptno
and e.sal>a.xx
and e.sal>=s.losal
and e.sal<=s.hisal;
–14:列出与SCOTT从事相同工作的所有员工及部门名称,部门人数
select e.job from emp e where e.ename = 'SCOTT';
select * from dept;
select e.ename,d.dname,count(d.deptno)
from emp e,dept d,
(select e.job from emp e where e.ename = 'SCOTT') t
where e.deptno = d.deptno(+)
and e.job = t.job
group by e.ename,d.dname;
–19. 求每个部门中的薪水最高的人
select e.ename,d.dname,e.deptno,e.sal
from scott.emp e,scott.dept d,(select e1.deptno ,max(e1.sal) msal
from scott.emp e1
group by e1.deptno) t1
where e.deptno=d.deptno
and e.sal=t1.msal
–21. 求每个部门的平均薪水的等级
select d.dname,e.deptno ,s.grade
from scott.emp e,scott.dept d,scott.salgrade s,
(select e1.deptno,avg(e1.sal) asal from scott.emp e1 group by e1.deptno) t1
where e.deptno=d.deptno
and t1.asal>=s.losal
and t1.asal<=s.hisal
group by d.dname,e.deptno,s.grade;
–22. 求每个部门的平均的薪水
select d.dname,e.deptno,avg(e.sal) asal
from scott.emp e,scott.dept d
where e.deptno=d.deptno
group by e.deptno,d.dname;
–23. 求雇员中有哪些人是经理人
select e.ename
from scott.emp e
where e.job='MANAGER';