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)
);


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','pretty','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<a.sal) as 人数 from emp as a
where (select count(b.ename) from emp as b where b.sal<a.sal)>=5

你可能感兴趣的:(sql,C++,c,工作,C#)