二、简答题,按要求写出SQL语句,使用SQL标准语句
Create table dept
(
Deptno varchar(10) primary key
Dname varchar(10)
);
Create table emp
(
Empno varchar(10) primary key
Ename varchar(10)
Job varchar(10)
Mgr varchar(10)
Sal varchar(10)
Deptno varchar(10) references dept(deptno)
);
Drop table dept;
Drop table emp;
Insert into dept values(‘1’,’事业部’);
Insert into dept values(‘2’,’销售部’);
Insert into dept values(’3’,’技术部’);
Insert into emp values(‘01’,’jacky’,’clerk’,’tom’,’1000’,’1’);
Insert into emp values(‘02’,’tom’,’clerk’,’’,’2000’,’1’);
Insert into emp values(‘07’,’biddy’,’clerk’,’’,’2000’,’1’);
Insert into emp values(‘03’,’jenny’,’sales’,’prety’,’600’,’2’);
Insert into emp values(‘04’,’pretty’,’sales’,’’,’800’,’2’);
Insert into emp values(‘05’,’buddy’,’jishu’,’canndy’,’1000’,’3’);
Insert into emp values(‘06’,’canndy’,’jishu’,’’,’1500’,’3’);
Select*from dept;
Select*from emp;
1、 列出emp表中各部门的部门号,最高工资,最低工资
select deptno as 部门号,max(sal) as 最高工资,min(sal) as 最低工资 from
emp group by deptno;
2、 列出emp表中各部门job为’CLERK’的员工的最低工资,最高工资。
select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp where
job='clerk' group by deptno;
3、 对于emp中最低工资小于2000的部门。列出job为’CLERK’的员工的部门号,最低工资,最高工资
select b.deptno as 部门号,max(sal) as 最高工资,min(sal) as 最低工资 from emp as b where job='clerk' and (select min(sal)from emp as a where a.deptno=b.deptno)<2000 group by b.deptno;
4、 根据部门号由高到低,工资由低到高对应每个员工的姓名,部门号,工资
select ename as 姓名,deptno as 部门号,sal as 工资 from emp order by deptno desc,sal asc;
5、 列出’buddy’所在部门中每个员工的姓名与部门号
select b.ename as 姓名,b.deptno as 部门号 from emp as b where b.deptno= (select a.deptno from emp as a where a.ename='buddy');
6、 列出每个员工的姓名,工资,部门号,部门名
select ename as 姓名,job as 工作,dept.deptno as 部门号,dept.dname as 部门名 from emp,dept where emp.deptno=dept.deptno;
7、 列出emp中工作为’CLERK’的员工的姓名,工作,部门号,部门名
select ename as 姓名,job as 工作,dept.deptno as 部门号,dept.dname as 部门名 from emp,dept where emp.deptno=dept.deptno and job='clerk';
8、 对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)
select a.deptno as 部门号,a.ename as 员工,b.ename as 管理者 from emp as a,emp as b where a.mgr is not null and a.mgr=b.ename;
9、 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为’CLERK’
的员工名与工作
select a.deptno as 部门号,a.dname as 部门名,b.ename as 员工名,b.job as 工作 from dept as a, emp as b where a.deptno=b.deptno and b.job='clerk';
10、对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
select b.deptno as 部门号,b.ename as 姓名,b.sal as 工资 from emp as b where b.sal>(select avg(a.sal) from emp as a where a.deptno=b.deptno) order by b.deptno;
11、对于emp,列出各个部门中工资高于本部门平均工资的员工数和部门号,按部门号排序
select a.deptno as 部门号,count(a.sal) as 员工数 from emp as a where a.sal>(select avg(b.sal) from emp as b where a.deptno=b.deptno) group by a.deptno order by a.deptno;
12、对于emp中工资高于本部门平均水平,人数多于1人的,列出部门号,人数,平均工资,按部门号排序
select count(a.empno) as 员工数,a.deptno as 部门号,avg(sal) as 平均工资 from emp as a where (select count(c.empno) from emp as c where c.deptno=a.deptno and c.sal>(select avg(sal) from emp as b where c.deptno=b.deptno))>1 group by a.deptno order by a.deptno;
13、对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
select a.deptno as 部门号,a.ename as 姓名,a.sal as 工资,(select count(b.ename) from emp as b where b.sal 因篇幅问题不能全部显示,请点此查看更多更全内容