表结构
员工表
emp表结构如下:
Name | Type | Nullable | Default | Comments |
---|---|---|---|---|
EMPNO | NUMBER(4) | 员工号 | ||
ENAME | VARCHAR2(10) | Y | 员工姓名 | |
JOB | VARCHAR2(9) | Y | 工作 | |
MGR | NUMBER(4) | Y | 上级编号 | |
HIREDATE | DATE | Y | 雇佣日期 | |
SAL | NUMBER(7,2) | Y | 薪金 | |
COMM | NUMBER(7,2) | Y | 佣金 | |
DEPTNO | NUMBER(2) | Y | 部门编号 |
部门表
dept表结构如下:
Name | Type | Nullable | Default | Comments |
---|---|---|---|---|
DEPTNO | NUMBER(2) | 部门编号 | ||
DNAME | VARCHAR2(14) | Y | 部门名称 | |
LOC | VARCHAR2(13) | Y | 地点 |
提示:工资 = 薪金 + 佣金
问题
1.列出至少有一个员工的所有部门
--第一种
select dname from dept where deptno in (select deptno from emp);
--第二种
select dname from dept where deptno in (select deptno from emp group by deptno having count(deptno) >=1);
--第三种
select dname from dept a where exists (select deptno from emp b where b.deptno = a.deptno );
2.列出薪金比“SMITH”多的所有员工
--第一种
select * from emp where sal > (select sal from emp where ename = 'SMITH');
--第二种
select * from emp a,(select sal from emp where ename = 'SMITH') b where a.sal > b.sal ;
--第三种
select * from emp a where exists (select null from emp b where a.sal>b.sal and b.ename = 'SMITH');
3.列出所有员工的姓名及其直接上级的姓名
--第一种
select a.ename,b.ename as boos_name from emp a left join emp b on a.mgr = b.empno;
--第二种
select a.ename,(select ename from emp b where b.empno = a.mgr) as boos_name from emp a;
4.列出受雇日期早于其上级的所有员工
--第一种
select a.* from emp a inner join emp b on b.empno = a.mgr where a.hiredate<b.hiredate;
--第二种
select a.* from emp a where exists (select null from emp b where b.empno = a.mgr and a.hiredate<b.hiredate);
5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
--第一种
select a.dname,deptno,b.empno,b.ename,b.job,b.mgr,b.hiredate,b.sal,b.comm from dept a left join emp b using(deptno);
--第二种
select a.dname,b.* from dept a left join emp b on b.deptno = a.deptno;
6.列出所有“CLERK"(办事员)的姓名及其部门名称
--第一种
select a.ename,b.dname from emp a left join dept b on b.deptno=a.deptno where a.job='CLERK';
--第二种
select dname,ename from (select ename,deptno from emp where job= 'CLERK') left join dept using(deptno);
7.列出最低薪金大于1500的各种工作
--第一种
select job from emp group by job having min(sal)>1500;
--第二种
select distinct job from emp a where a.sal>1500 and not exists(select null from emp b where a.job = b.job and a.sal>b.sal);
8.列出在部门“SALES"(销售部)工作的员工的姓名,假定不知道销售部的部门编号
--第一种
select ename from emp a inner join dept b on a.deptno = b.deptno and b.dname='SALES';
--第二种
select ename from emp a where a.deptno = (select deptno from dept where dname = 'SALES');
--第三种
select ename from emp a where exists (select null from dept b where a.deptno = b.deptno and b.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');
--第二种
select * from emp a where exists (select null from emp b where a.job=b.job and b.ename='SCOTT');
11.列出薪金等于部门编号为30中员工的薪金的所有员工的姓名和薪金
--第一种
select a.ename,a.sal from emp a where a.deptno <> 30 and exists (select null from emp b where b.deptno=30 and a.sal=b.sal);
--第二种
select a.ename,a.sal from emp a where a.sal in (select sal from emp where deptno=30) and a.deptno<> 30;
12.列出薪金高于在部门编号为30工作的所有员工的薪金的员工姓名和薪金
select ename,sal from emp where sal>(select max(sal) from emp where deptno=30);
13.列出在每个部门工作的员工数量、平均薪金
select (select b.dname from dept b where b.deptno=a.deptno) as dname,count(deptno) as deptcount,avg(sal) as deptavgsal from emp a group by deptno;
14.列出所有员工的姓名、部门名称和工资(工资 = 薪金 + 佣金)
select a.ename,b.dname,a.sal+nvl2(a.comm,a.comm,0) as wage from emp a left join dept b on a.deptno= b.deptno;
15.列出所有部门的详细信息和部门人数
--第一种
select a.*,(select count(deptno) from emp b where b.deptno = a.deptno group by b.deptno) as count from dept a;
--第二种
select deptno,DNAME,LOC,coalesce(emp_count,0) from dept left join (select count(deptno) emp_count,deptno from emp b group by b.deptno) a using(deptno);
16.列出各种工作的最低工资
--第一种
select job,min(sal) from emp group by job;
--第二种
select distinct job,sal from emp a where not exists(select null from emp b where a.job=b.job and a.sal>b.sal);
17.列出各个部门的MANAGER(经理)的最低薪金
--第一种
select a.deptno,min(sal) from emp a where a.job='MANAGER' group by a.deptno;
18.列出所有员工的年工资,按年薪从低到高排序
--第一种
select ename,(sal+nvl(comm,0))*12 as salpersal from emp order by salpersal
--第二种
select ename,(sal+coalesce(comm,0))*12 as salpersal from emp order by 2;
19.用一条SQL语句查询出emp表中每个部门工资前三位的数据,显示结果如下:
DEPTNO | SAL1 | SAL2 | SAL3 |
---|---|---|---|
10 | 5500 | 5500 | 5500 |
20 | 4000 | 3000 | 2975 |
30 | 2850 | 1600 | 1500 |
select deptno, max(sal) sal1, max(decode(id, 2, sal)) sal2, min(sal) sal3
from (select sal, deptno, id
from (select ename,
sal,
ROW_NUMBER() over(partition by deptno order by sal desc) id,
deptno
from emp) e
where e.id <= 3)
group by deptno;