mysql学习第四天(高级查询)

-- 第七章
-- 1、查询入职日期最早和最晚的日期
select min(hiredate),max(hiredate)
from emp

-- 2、查询职位以SALES开头的所有员工平均工资,最低工资,最
-- 高工资,工资和
select avg(sal),min(sal),max(sal),sum(sal)
from emp
where job like 'SALES%'

-- 3、查询部门30有多少个员工
select count(*)
from emp
where deptno = '30'

-- 4、查询有员工的部门数量
select count(distinct deptno)
from emp

-- 5、求奖金的平均值(包括没有奖金的人)
select avg(ifnull(comm,0))
from emp

-- 练习1
-- 1、查询部门20的员工,每个月的工资总和及平均工资
select sum(sal),avg(sal)
from emp
where deptno = 20
-- 2、查询工作在CHICAGO的员工人数,最高工资及最低工资
select count(*),max(sal),min(sal)
from emp,dept
where emp.deptno = dept.deptno
and loc = 'CHICAGO'
-- 3、查询员工表中一共有几种岗位类型
select count(distinct job)
from emp

 

-- 6、查询每个部门的编号,平均工资
select deptno,avg(sal)
from emp
group by deptno

-- 7、查询每个岗位的工资总和
select deptno,job,sum(sal)
from emp
group by deptno,job


-- 1、查询每个部门的部门号,部门名称,部门人数,最高工资,
-- 最低工资,工资总和,平均工资
select emp.deptno,dname,count(*),max(sal),min(sal),sum(sal),avg(sal)
from emp,dept
where emp.deptno = dept.deptno
group by emp.deptno,dname
-- 2、查询每个部门,每个岗位的部门编号,部门名称,
-- 岗位名称,部门人数,最高工资,最低工资,工资总和,
-- 平均工资
select emp.deptno,dname,job,count(*),max(sal),min(sal),sum(sal),avg(sal)
from emp,dept
where emp.deptno = dept.deptno
group by emp.deptno,dname,job
-- 3、查询每个经理所管理的人数,经理编号,经理姓名,
-- 要求包括没有经历的人员信息
select count(*),t1.mgr,t2.ename
from emp t1
left join emp t2
on t1.mgr = t2.empno
group by t1.mgr,t2.ename

-- 8、查询每个部门工资大于2900的部门编号,最高工资
select deptno,max(sal)
from emp
group by deptno
having max(sal) > 2900

-- 9、查询不是以SALES开头,工资总和大于5000的职位名称及
-- 工资总和以工资总和升序排序
select job,sum(sal)
from emp
where job not like 'SALES%'
group by job
having sum(sal) > 5000
order by sum(sal)


-- 练习3
-- 1、查询部门人数大于2的部门编号,部门名称,部门人数
select dept.deptno,dname,count(*)
from dept,emp
where dept.deptno = emp.deptno
group by dept.deptno,dname
having count(*) > 2
-- 2、查询部门平均工资大于2000,并且人数大于2的部门编号
-- 部门名称,部门人数,部门平均工资,并按照部门人数升序排列
select dept.deptno,dept.dname,count(*),avg(sal)
from dept,emp
where dept.deptno = emp.deptno
group by dept.deptno,dept.dname
having avg(sal) > 2000
and count(*) >2
order by count(*)

-- 10、查出比JONES工资高的其它员工
select *
from emp
where sal >(select sal
from emp
where ename = 'JONES')

-- 11、显示和雇员7369从事相同工作并且工资大于雇员7876的雇员的姓名和工作
select ename,job
from emp
where job = (select job
from emp
where empno = 7369)
and sal > (select sal
from emp
where empno = 7876)

-- 12、查询工资最低的员工姓名,岗位及工资
select ename,job,sal
from emp
where sal = (select min(sal)
from emp)
-- 13、查询部门最低工资比20部门最低工资高的部门编号及最低工资
select deptno,min(sal)
from emp
group by deptno
having min(sal) > (select min(sal)
from emp
where deptno = 20)


-- 练习4
-- 1、查询入职日期最早的员工姓名,入职日期
select ename,hiredate
from emp
where hiredate = (select min(hiredate)
from emp)
-- 2、查询工资比SMITH工资高并且工作地点在CHICAGO的员工姓名,工资,部门名称
select ename,sal,dname
from emp,dept
where emp.deptno = dept.deptno
and loc = 'CHICAGO'
and sal > (select sal
from emp
where ename = 'SMITH')
-- 3、查询入职日期比20部门入职日期早的员工还要早的员工姓名,入职日期
select min(hiredate)
from emp
group by deptno
having min(hiredate) < (select min(hiredate)
from emp
where deptno = 20)
-- 4、查询部门人数大于所有部门平均人数的部门编号,部门名称,部门人数
select dept.deptno,dept.dname,count(*)
from emp,dept
where emp.deptno = dept.deptno
group by deptno
having count(*) > (select count(*)/count(distinct deptno)
from emp)

select dept.deptno,dept.dname,count(*)
from emp,dept
where emp.deptno = dept.deptno
group by deptno
having count(*) > (select avg(t.count)
from (select count(*) count
from emp
group by deptno)t)


-- 14、查询是经历的员工姓名,工资
select distinct t2.ename,t2.sal
from emp t1,emp t2
where t1.mgr = t2.empno

select ename,sal
from emp
where empno in (select mgr
from emp)


-- 15、查询部门编号不为10,且工资比10部门任意一名员工工资高的员工编号,姓名,职位,工资
select empno,ename,job,sal
from emp
where deptno <> 10
and sal >any (select sal
from emp
where deptno = 10)

-- 16、查询部门编号不为20,且工资比20部门所有员工工资高的员工编号,姓名,职位,工资
select empno,ename,job,sal
from emp
where sal > all(select sal
from emp
where deptno = 20)
and deptno <> 20

-- 练习5
-- 1、查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工
select ename,hiredate
from emp
where hiredate <any (select hiredate
from emp
where deptno = 10)
and deptno <> 10
-- 2、查询入职日期比10部门所有员工晚的员工姓名,入职日期,不包括10部门员工
select ename,hiredate
from emp
where hiredate <all (select hiredate
from emp
where deptno = 10)
and deptno <> 10
-- 3、查询职位和10部门任意一个员工职位相同的员工姓名,职位,不包括10部门员工
select ename,job
from emp
where job in (select job
from emp
where deptno = 10)
and deptno <> 10


-- 17、查询不是经理的员工姓名
select ename
from emp
where empno not in(select mgr
from emp
where mgr is not null)


-- 18、查询比自己部门平均工资高的员工姓名,工资,部门编号,部门平均工资
select ename,sal,emp.deptno,avgsal
from emp,(select deptno,avg(sal) avgsal
from emp
group by deptno) t
where emp.deptno = t.deptno
and emp.sal > t.avgsal

select ename,sal,deptno
from emp t
where sal > (select avg(sal)
from emp
where deptno = t.deptno)

select ename,sal,deptno,(select avg(sal)
from emp
where deptno = t.deptno)
from emp t
where sal > (select avg(sal)
from emp
where deptno = t.deptno)

 

-- 查询员工姓名及其查询所在部门的人数
select ename,c
from emp,(select deptno,count(*) c
from emp
group by deptno) t
where emp.deptno = t.deptno

select ename,(select count(*)
from emp
where emp.deptno = t.deptno
group by emp.deptno)
from emp t

-- 查询是所在部门工资最低的员工信息
select *
from emp
where (deptno,sal) in (select deptno,min(sal)
from emp
group by deptno)

select *
from emp,(select deptno,min(sal) minsal
from emp
group by deptno)t
where emp.deptno = t.deptno
and sal = minsal


select *
from emp t
where sal = (select min(sal)
from emp
where deptno = t.deptno
group by deptno)

posted @ 2018-08-02 20:40  MKN  阅读(790)  评论(0编辑  收藏  举报