-
每个部门的人数,根据人数降序排序
select deptno,count(*) c from emp group by deptno order by c desc;
- 正确写法
select d.deptno,count(e.empno) c
from emp e right join dept d
on e.deptno=d.deptno group by d.deptno order by c desc;
-
每个部门中,每个主管的手下人数
select deptno,mgr,count(*) from emp
where mgr is not null group by deptno,mgr;
-
每种工作的平均工资
select job,avg(sal) from emp group by job;
-
每年的入职人数
select extract(year from hiredate) year,count(*) from emp
group by year;
-
少于等于3个人的部门信息
- 得到部门编号
select deptno from emp group by deptno having count(*)<=3;
- 通过部门编号查部门信息
select * from dept where deptno in(select deptno from emp group by deptno having count(*)<=3);
- 包含40号部门的写法:
select d.*
from emp e right join dept d
on e.deptno=d.deptno group by d.deptno
having count(e.empno)<=3;
-
拿最低工资的员工信息
select * from emp where sal=(select min(sal) from emp);
-
只有一个下属的主管信息
-得到主管编号
select * from emp where mgr is not null
group by mgr having count()=1;
-通过主管编号查询详细信息
select * from emp where empno in(select mgr from emp where mgr is not null
group by mgr having count()=1);
-
每月发工资最多的部门信息
- 得到部门最高的工资总和
select sum(sal) from emp group by deptno order by sum(sal) desc limit 0,1;
- 通过最高的工资总和查询部门编号
select deptno from emp group by deptno having sum(sal)=(select sum(sal) from emp group by deptno order by sum(sal) desc limit 0,1);
- 通过部门编号得到部门详情
select * from dept where deptno in(上面一坨);
-
下属最多的人,查询其个人信息
-得到最多的人数
select count() from emp group by mgr order by count() desc limit 0,1;
-通过最大人数得到领导的编号
select mgr from emp group by mgr having count(*)=(select count() from emp group by mgr order by count() desc limit 0,1);
- 通过领导编号得到个人详情
select * from emp where empno in(上面一坨);
-
拿最低工资员工的同事信息
select min(sal) from emp;
select job from emp where sal=(select min(sal) from emp);
select * from emp where job=(select job from emp where sal=(select min(sal) from emp)) and sal!=(select min(sal) from emp);
-
和最后入职的员工在同一部门的员工信息
select max(hiredate) from emp;
select deptno from emp where hiredate=(select max(hiredate) from emp);
select * from emp where deptno=(第二坨) and hiredate!=(第一坨);
-
查询平均工资高于20号平均工资的部门信息
select avg(sal) from emp where deptno=20;
select d.*
from emp e join dept d
on e.deptno=d.deptno
group by d.deptno
having avg(sal)>(select avg(sal) from emp where deptno=20);
-
查询员工信息和员工对应的部门名称
select e.*,d.dname
from emp e join dept d
on e.deptno=d.deptno;
-
查询员工信息,部门名称,所在城市
select e.*,d.dname,d.loc
from emp e join dept d
on e.deptno=d.deptno;
-
查询Dallas市所有的员工信息
select e.*
from emp e join dept d
on e.deptno=d.deptno where d.loc='dallas';
-
计算每个城市的员工数量
select d.loc,count(e.ename)
from emp e right join dept d
on e.deptno=d.deptno
group by d.loc;
-
查询员工信息和他的主管姓名
select e.ename,m.ename
from emp e join emp m
on e.mgr=m.empno;
-
员工信息,员工主管名字,部门名
select e.ename,m.ename,d.dname
from emp e join emp m
on e.mgr=m.empno
join dept d
on e.deptno=d.deptno;
-
员工名和他所在部门名
select e.ename,d.dname
from emp e join dept d
on e.deptno=d.deptno;
-
案例:查询emp表中所有员工的姓名以及该员工上级领导的编号,姓名,职位,工资
select e.ename,m.empno,m.ename
from emp e left join emp m
on e.mgr = m.empno;
-
案例:查询emp表中名字中没有字母'K'的所有员工的编号,姓名,职位以及所在部门的编号,名称,地址
insert into emp (empno,ename) values(10010,'苍老师');
select e.empno,e.ename,e.job,d.*
from emp e left join dept d
on e.deptno=d.deptno
where e.ename not like '%k%';
-
案例:查询dept表中所有的部门的所有的信息,以及与之关联的emp表中员工的编号,姓名,职位,工资
select d.*,e.empno,e.ename,e.job,e.sal
from emp e right join dept d
on e.deptno=d.deptno;