注:以下查询基于SCOTT模式中的表
1.查询每个雇员的编号、姓名、工作,并为这些列各起相应的中文别名。
select EMPNO 编号,ENAME 姓名,JOB 职业 from scott.emp;
2.查询所有工种不是MANAGER和CLERK,且工资大于或等于2000的员工的详细信息。
select * from scott.emp
2 where job NOT IN(
3 'MANAGER','CLERK'
4 ) and sal>=2000;
3.查询工资介于1500和3000的雇员信息。
select * from scott.emp
2 where sal between 1500 and 3000;
4.查询出雇员姓名中第二个字母为M的雇员信息。
select * from scott.emp
2 where ENAME like '_M%';
5.查询雇员的信息,并按工资从高到低排序,若工资相同,则按员工编号排序。
select * from scott.emp order by sal desc,EMPNO;
6.查询所有雇员的编号、姓名、工资、部门编号、部门名称、工资在公司的等级(salgrade表)信息。
select e.empno 编号,e.ename 姓名,e.sal 工资,d.deptno 部门编号,d.dname 部门名称,g.grade 工资等级
2 from scott.emp e,scott.dept d,scott.salgrade g
3 where (e.deptno = d.deptno) and (e.sal between g.losal and g.hisal);
7.查找雇员的编号、姓名,及其领导的编号、姓名。(注:使用自连接)
select distinct w.empno 雇员编号,w.ename 雇员姓名,m.mgr 领导编号,m.ename 领导姓名
2 from scott.emp w,scott.emp m
3 where w.empno = m.mgr;
8.查询每个部门的平均工资。
select deptno 部门,avg(sal) 平均工资 from scott.emp group by deptno;
9.查询平均工资大于2000的部门编号和平均工资。(注:使用HAVING子句)
select deptno 部门,avg(sal) 平均工资
2 from scott.emp
3 group by deptno
4 having avg(sal)>2000;
10.查询每个部门的员工数量、部门的平均工资、以及部门的名称。
select deptno 部门,count(*) 员工数量,avg(sal) 平均工资
2 from scott.emp
3 group by deptno;
11.查询最少有一个员工的部门信息
select dname from scott.dept where deptno in(
2 select deptno from scott.emp group by deptno
3 having count(deptno)>=1
);
12.查询出比7654号雇员的工资要高的全部雇员。
select EMPNO 编号,ENAME 姓名
2 from scott.emp
3 where sal > (select sal from scott.emp where EMPNO = 7654);
13.查询工资高于公司平均工资的所有员工的信息。
select * from scott.emp
2 where sal > (select AVG(sal) from scott.emp);
14.查询各个部门中不同职位的最高工资。
select max(sal) 最高工资,deptno 部门号,job 职业
2 from scott.emp group by deptno,job order by deptno;
15.查询雇员编号、姓名、雇佣日期,其中雇佣日期以’YYYY-MM-DD’的格式显示。
select EMPNO 雇员编号,ENAME 姓名,
to_char(HIREDATE,'YYYY-MM-DD') 雇佣日期
2 from scott.emp;
如有不当之处,还请大家多多指教,谢谢!