01. 查询员工表所有数据,并说明使用*的缺点
select * from emp;
01.查询职(job)为'PRESIDENT'的员工的工资
select sal from emp where job='PRESIDENT';
02.查询佣金为0或为null的员工的信息
select * from emp where comm is null or comm=1;
03.查询入职日期在1981-5-1到1981-12-31之间的所有员工的信息
select * from emp where hiredate between to_date(‘1-5月-81’) and to_date(‘31-12月-81’)
04..查询所有名字长度为4的员工的员工的编号,姓名
select empno,ename from emp where length(ename)=4
05.显示10号部门饿所有经理和20号部门的所有员工
select * from emp where job='MANAGER' and deptno=10 or job='CLERK' and deptno=20;
06.显示姓名没有'L'字的员工的详细信息或含有'SM'字的员工信息
select * from emp where ename not like '%L%' or ename like '%SM%';
07.显示各个部门经理的工资
select deptno,sal from emp where job='MANAGER';
08.显示佣金收入比工资高的员工的详细信息
select * from emp where comm>sal;
10.把hiredate列看做是员工的生日,求本月过生日的员工:
SQL> select * from emp
where to_char(hiredate,'mm')=to_char(sysdate,'mm');
11.把hiredate列看做是员工的生日,求下月过生日的员工
SQL> select * from emp
where to_char(hiredate,'mm')=to_char(add_months(sysdate,1
),'mm');
12.求1982年入职的员工
SQL> select * from emp where to_char(hiredate,'yyyy')='1982';
hiredate是date类型的,1982是字符串类型的,类型匹配才可以
13.求1981年下半年入职的员工
select * from emp
where hiredate between to_date('1981-7-1','yyyy-mm-dd')
and to_date('1982-1-1','yyyy-mm-dd')-1;
注:to_char()函数和to_date()函数
to_char()函数是把日期的类型转换为指定的格式
to_date()是把字符串转换为日期类型
-1的原因是更精确
14.求1981年各个月入职的员工个数
SQL> select to_char(hiredate,'mm'),count(*) from emp where to_char(hiredate,'yyy
y')='1981' group by to_char(hiredate,'mm') order by to_char(hiredate,'mm');
或
select to_char(hiredate,'mm'),count(*) from emp where to_char(hiredate,'yyyy')='1981' group by to_char(hiredate,'mm') order by to_char(hiredate,'mm');
PartII
01 .查询各个部门的平均工资
SQL> select deptno,avg(nvl(sal,0)) from emp group by deptno;
02.显示各种职位的最低工资
SQL> select job,min(sal) from emp group by job;
03.按照入职日期由新到旧排列员工信息
SQL> select hiredate from emp order by hiredate desc;
04.查询员工的基本信息,附加其上级的姓名(自关联)
SQL> select e.*,e1.ename from emp e,emp e1 where e.mgr=e1.empno;
05.显示工资比’ALLEN’高的所有员工的姓名和工作
SQL> select ename,sal from emp where sal>(
select sal from emp where ename='ALLEN');
06.显示与scott从事相同工作的员工的信息(子查询)
SQL> select * from emp where job=(
select job from emp where ename='SCOTT');
07.显示销售部(‘SALES’)员工的姓名
SQL> select e.ename from emp e inner join dept d on e.deptno=d.deptno where d.dname='SALES';
08.显示与30号门’MARTIN’员工工资相同的员工的姓名和工资
SQL> select ename,sal from emp where sal=(select sal from emp where deptno=30 and ename='MARTIN');
09.查询所有工资高于平均工资(包括所有员工)的销售人员
SQL> select * from emp where sal>(
select avg(sal) from emp) and job='SALESMAN';
或
SQL> select * from emp where job='SALESMAN' and sal>(
select avg(sal) from emp);
10.显示所有职员的姓名及其所在部门的名称和工资(表连接)
SQL> select e.*,e.sal,d.dname from emp e inner join dept d on e.deptno=d.deptno;
11.查询在研发部(RESEARCH)工作人员的编号,姓名,工作部门,工作所在地
SQL> select e.empno,e.ename,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno where dname='RESEARCH';
12.查询各个部门的名称和员工人数
select e.deptno,d.dname,count(*) from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname
分析:
SQL> select d.dname from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname;
DNAME
--------------
ACCOUNTING
RESEARCH
SALES
SQL> select e.deptno,d.dname from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname;
DEPTNO DNAME
------ --------------
10 ACCOUNTING
20 RESEARCH
30 SALES
SQL> select e.deptno,d.dname,count(*) from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname;
DEPTNO DNAME COUNT(*)
------ -------------- ----------
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
13.查询各个部门员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位(子查询)
SQL> select count(*),job from emp where sal>(select avg(sal) from emp) group by job;
14.查询工资相同的员工的工资和姓名(子查询)
SQL> select sal,ename from emp e where(select count(*) from emp where sal=e.sal group by sal)>1;
或
SQL> select e.sal,e.ename from emp e,emp e1 where e.sal=e1.sal and e.ename<>e1.ename;
15.查询工资最高的3名员工信息(排序)
SQL> select rownum,e1.* from (
select e.* from emp e order by sal desc) e1
where rownum<=3;
rownum是伪列,记性排序的
16.按工资进行排名:排名从1开始,工资相同排名相同(如果两个并列第1则没有地2名,从第三名继续排)
SQL> select e.*,(select count(*) from emp where sal>e.sal)+1 rank from emp e order by rank;
或 select sal,rank() over(order by sal desc) from emp;
17.求入职日期相同的(年月日相同)的员工
SQL> select * from emp e where(select count(*) from emp where e.hiredate=hiredate)>1;
或
SQL> select * from emp e,emp e1 where e.hiredate=e1.hiredate and e.empno<>e1.empno;
18.查询每个部门的最高工资
SQL> select max(sal),deptno from emp group by deptno order by deptno;
19.查询每个部门,每个职位的最高工资
SQL> select deptno,job,max(sal) from emp group by deptno,job order by deptno;
20.查询每个员工的信息及工资级别,用到表(Salgrade)
SQL> select e.ename,s.grade from emp e,salgrade s where sal between losal and hisal;
或
select e.*,s.grade from emp e inner join salgrade s on e.sal between s.losal and s.hisal
21.查询工资最高的第6-10名员工
SQL> select * from (select rownum as r,e.* from (select * from emp order by sal desc) e) e1 where e1.r>=6 and e1.r<=10;
或
SQL> select * from(
select rownum as r,e.* from emp e order by sal desc) e1
where e1.r>=6 and e1.r<=10;
或
select * from (select rownum r,e1.* from (select e.* from emp e order by sal desc) e1 where rownum<10) e2 where e2.r>=6
结果:
R SAL
---------- -----
6 2450
7 1600
8 1500
9 1300
10 1250
Order by不能和rownum合用,
可以做分页啦!!
22.查询各个部门工资最高的员工信息
SQL> select * from emp e where e.sal=(
select max(sal) from emp where (deptno=e.deptno));
或
select * from emp e,(select deptno,max(sal) maxSal from emp group by deptno) e1 where e.sal=maxSal and e.deptno=e1.deptno
或
select * from emp e inner join (select deptno,max(sal) maxSal from emp group by deptno) e1 on e.sal=maxSal and e.deptno=e1.deptno
23.查询每个部门工资最高的前2名员工
SQL> select * from emp e where (
select count(*) from emp where sal>e.sal and e.deptno=deptno)<2
order by deptno,sal desc;
24.查询出有3个以上下属的员工信息(自关联)
select * from emp e where (
select count(*) from emp where e.empno=mgr)>2
25.查询所有大于本部平均工资的员工信息
SQL> select * from emp e where sal>(
select avg(sal) from emp where e.deptno=deptno);
或
select * from emp e,(select deptno,avg(sal) avgSal from emp group by deptno) e1 where e.sal>avgSal and e.deptno=e1.deptno;
26.查询平均工资最高的部门信息
select d.* from dept d,(select deptno,avg(sal) av from emp group by deptno) where av=(select max(avg(sal)) from emp group by deptno) and e.deptno=d.deptno
或
select d.* from dept d where deptno=(select e.deptno from (select max(avgSal) maxSal from (select deptno,avg(nvl(sal,0)) avgSal from emp group by deptno)) e1 inner join (select deptno,avg(nvl(sal,0)) avgSal from emp group by deptno) e on e.avgSal=e1.maxSal)
注:如果在当前行的字段在查询的时候不能用别名:
select dname,deptno dp from dept where dp=10
ORA-00904: "DP": 标识符无效
SQL> select dname,deptno dp from dept where deptno=10;
DNAME DP
-------------- ---
ACCOUNTING 10
也不能使用在inner join on条件中,可以使用在order by中,
别名放在from子句(select 。。。。别名 使用from)之后,这个别名可以在任何位置
在having中也不能使用别名
27.查询大于各个部门总工资的平均值的部门信息
select d.*,sumsal from dept d,(select sum(nvl(sal,0)) sumsal,deptno from emp group by deptno) sumS where sumSal>(select avg(sum(nvl(sal,0))) from emp group by deptno) and sumS.deptno=d.deptno
28.查询大于各个部门总工资平均值的部门下的员工信息
select e.* from emp e,(select sum(nvl(sal,0)) sumSal,deptno from emp group by deptno) e1 where
sumSal>(select avg(sum(nvl(sal,0))) from emp group by deptno) and e1.deptno=e.deptno
29.查询么有员工的部门信息
select * from dept where deptno not in(select deptno from emp group by deptno);