1、查询emp中最高薪水人的名字
2、查询每个部门中的最高薪水人的名字和所在的部门编号
3、查询薪水在平均薪水之上的雇员的名字
4、查询雇员的名字和所在部门的名字
5、查询薪水在在本部门平均薪水之上的雇员的名字
6、查询每个员工的薪水的等级,员工的姓名
7、查询每个部门的平均薪水的等级,部门的编号
8、查询雇员的名字,所在部门的名字,工资的等级
9、查询雇员的名字和其经理的名字
10、查询雇员中是经理人的名字
11、查询平均薪水最高的部门的编号和名称
12、查询薪水最高的前5名雇员编号,名称,薪水
13、查询薪水最高的第6名到第10名雇员编号,名称,薪水
14、查询部门的名字和部门的人数(如果部门里没有人数,显示0个)
15、查询员工的编号,工资和所在部门的平均工资
/*1、查询emp中最高薪水人的名字*/
#1、查询最高的薪水
select max(sal) from EMP;
#2、将1作为条件
select
ename
from
EMP
where
sal = (select max(sal) from EMP);
/*2、查询每个部门中的最高薪水人的名字和所在的部门编号*/
#1、各个部门的最高薪水
select
max(sal),deptno from EMP
group
by deptno;
#2、将1的结果作为一个新的表,联表查询
select
t1.ename,t1.deptno
from
EMP t1
inner join
(select max(sal) max_sal,deptno from EMP group by deptno) t2
on
t1.deptno = t2.deptno and sal = t2.max_sal;
/*3、查询薪水在平均薪水之上的雇员的名字*/
select ename from EMP where sal >(select avg(sal) from EMP);
/*4、查询雇员的名字和所在部门的名字*/
select
t1.ename,t2.dname
from
EMP t1,DEPT t2
where
t1.deptno = t2.deptno;
/*5、查询薪水在在本部门平均薪水之上的雇员的名字*/
select
t1.ename,t1.deptno
from
EMP t1
inner join
(select avg(sal) avg_sal,deptno from EMP group by deptno) t2
on t1.deptno = t2.deptno and sal > t2.avg_sal;
/*6、查询每个员工的薪水的等级,员工的姓名*/
select * from EMP;
select * from SALGRADE;
select t1.ename,t2.grade
from EMP t1,SALGRADE t2
where t1.sal between t2.losal and t2.hisal;
/*7、查询每个部门的平均薪水的等级,部门的编号*/
#1、各个部门的平均薪水
select avg(sal),deptno from EMP group by deptno;
#2、将1的结果当作一个表
select
t1.grade,t2.deptno
from
SALGRADE t1,(select avg(sal) avg_sal,deptno from EMP group by deptno) t2
where
t2.avg_sal between t1.losal and t1.hisal
/*8、查询雇员的名字,所在部门的名字,工资的等级*/
select t1.ename,t2.dname,t3.grade
from
EMP t1,DEPT t2,SALGRADE t3
where
t1.deptno = t2.deptno
and
t1.sal between t3.losal and t3.hisal;
select t1.ename,t2.dname,t3.grade
from
EMP t1
inner join
DEPT t2
on
t1.deptno = t2.deptno
inner join
SALGRADE t3
on
t1.sal between t3.losal and t3.hisal;
/*9、查询雇员的名字和其经理的名字*/
select employee.ename,employer.ename
from EMP employer,EMP employee
where employee.mgr = employer.empno;
/*10、查询雇员中是经理人的名字*/
#1、经理上的编号
select distinct mgr from EMP;
#2、将1当作条件
select
ename
from
EMP
where
empno in(select distinct mgr from EMP);
/*11、查询平均薪水最高的部门的编号和名称*/
#1、每个部门的平均薪水
select avg(sal) avg_sal,deptno from EMP group by deptno;
#2、求1表中的最高平均薪水
select max(t.avg_sal) from (select avg(sal) avg_sal,deptno from EMP group by deptno) t
#3、将2的结果当作条件
select t1.deptno
from (select avg(sal) avg_sal,deptno from EMP group by deptno) t1
where t1.avg_sal = (select max(t2.avg_sal) from (select avg(sal) avg_sal,deptno from EMP group by deptno) t2)
#4、将3作为条件
select t.deptno,t.dname
from DEPT t
where deptno in (
select t1.deptno
from (select avg(sal) avg_sal,deptno from EMP group by deptno) t1
where t1.avg_sal = (select max(t2.avg_sal) from (select avg(sal) avg_sal,deptno from EMP group by deptno) t2)
);
/*12、查询薪水最高的前5名雇员编号,名称,薪水*/
select empno,ename,sal
from EMP
order by sal desc
limit 5;
/*13、查询薪水最高的第6名到第10名雇员编号,名称,薪水*/
select empno,ename,sal
from EMP
order by sal desc
limit 5,5;
/*14、查询部门的名字和部门的人数(如果部门里没有人数,显示0个)*/
select
t1.dname,ifnull(t2.num,0)
from
DEPT t1
left join
(select count(*) num,deptno from EMP group by deptno) t2
on
t1.deptno = t2.deptno;
/*15、查询员工的编号,工资和所在部门的平均工资*/
select
empno,ename,sal,t2.deptno,(select avg(sal) from EMP t1 where t1.deptno = t2.deptno)
from
EMP t2;
select
empno,ename,sal,t2.deptno,t2.avg_sal
from
EMP t1
inner join
(select avg(sal) avg_sal,deptno from EMP group by deptno) t2
on
t1.deptno = t2.deptno;