MySQL之多表查询3

有些话、适合烂在心里 提交于 2019-12-17 20:27:07

1.列出至少有四位员工的所有部门信息

1.查询每个部门的人数
     select deptno,count(*) from emp group by deptno

2.至少有四位
     select deptno,count(*) from emp group by deptno having count(*)>4

3.部门信息
     select d.*,dd.cou
       from dept d,(select deptno,count(*) cou from emp group by deptno having count(*)>4)dd
      where d.deptno=dd.deptno

2.列出薪金比“SMITH”多的员工信息

1.查询“SMITH”的薪金
    select sal from emp where ename='smith'

2.查询比子查询结果工资高的雇员信息
    select * from emp where sal>(select sal from emp where ename='smith')
 

3.列出所有员工的姓名及其直接上级的姓名。没有领导的雇员也列出

 select  e.ename,m.ename
    from emp e left join emp m
    on e.mgr=m.empno

4.列出受雇日期早于其直接上级的所有员工的编号、姓名、部门名称

select  e.empno,e.ename
      from emp e join emp m  on e.mgr=m.empno
   where e.hiredate<m.hiredate

2.连接部门表

  select  e.empno,e.ename,d.dname
      from emp e join emp m  on e.mgr=m.empno
         join dept d on e.deptno=d.deptno
   where e.hiredate<m.hiredate

5.列出所有“CLERK”(办事员)的姓名及其部门名称、部门人数

 1.所有“CLERK”(办事员)的姓名
     select ename from emp where job='clerk'

   2.部门人数
      select count(*),deptno from emp group by deptno

   3.select d.dname,dd.cou,e.ename
       from dept d join (select count(*) cou,deptno from emp group by deptno)dd on d.deptno=dd.deptno
            join emp e on d.deptno=e.deptno
        where job='clerk'

6.列出最低薪金大于1500的各种工作及此从事此工作的全部雇员人数

 1.最低薪金大于1500的各种工作
      select job from emp group by job having min(sal)>1500

   2.每个工作的雇员人数
     select  count(*) from emp group by job

   3. select  count(*),job from emp
      where job in(select job from emp group by job having min(sal)>1500)
     group by job 

7.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。

1.部门“SALES”(销售部)的部门号
    select deptno from dept where dname='sales'

  2.查询子查询结果的雇员姓名
     select ename
       from emp 
       where deptno=(select deptno from dept where dname='sales')

8.列出薪金高于公司平均薪金的所有员工,所在部门、上级领导

1.薪金高于公司平均薪金的所有员工
     select e.*
       from emp e
       where sal>(select avg(sal) from emp)

   2.连接部门表
     select e.*,d.*
       from emp e join dept d on e.deptno=d.deptno
       where sal>(select avg(sal) from emp)

   3.上级领导连接emp 
     select e.*,d.*,m.*
       from emp e join dept d on e.deptno=d.deptno
         join emp m on e.mgr=m.empno
       where e.sal>(select avg(sal) from emp)

9.列出与“SCOTT”从事相同工作的所有员工及部门名称

1.“SCOTT”从事工作
     select job from emp where ename='scott'

  2.从事子查询结果的所有员工
    select e.*
       from emp e
       where job=(select job from emp where ename='scott') and e.ename!='scott'

   3.连接部门表查询部门名称
     
   select e.*,d.dname
       from emp e join dept d on e.deptno=d.deptno
       where job=(select job from emp where ename='scott') and e.ename!='scott'

10.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金、部门

1.部门30的薪资
     select sal from emp where deptno=30

  2.高于部门30工作的所有员工的薪金
    select e.ename,e.sal
     from emp e
     where e.sal > all(select sal from emp where deptno=30)

  3.部门连接部门表
     select e.ename,e.sal,d.dname
     from emp e join dept d on e.deptno=d.deptno
     where e.sal > all(select sal from emp where deptno=30)

11.列出在每个部门工作的员工数量、部门名称、平均工资和平均服务期限,

 平均服务期限--服务条数datediff
   select datediff(sysdate(),hiredate)/365 from emp
   1.部门表和雇员表连表查询
     select  avg(sal),d.dname,count(*),avg(datediff(sysdate(),hiredate)/365)
      from dept d join emp e on d.deptno=e.deptno
    group by d.dname

12.列出所有员工的年工资,按年薪从低到高排序

1.员工的年工资
     select (sal+ifnull(comm,0))*12 yearsal from emp;

  2.年薪从低到高排序
    select (sal+ifnull(comm,0))*12 yearsal from emp order by yearsal;

13.求出部门名称中,带‘s’字符的部门员工的工资合计、部门人数

1. 部门名称中,带‘s’字符的部门的部门号
      select deptno from dept where dname like '%s%'
    2.每个工资合计、部门人数
      select sum(sal),count(*) from emp group by deptno

    3. select sum(sal),count(*),deptno from emp 
       where deptno in(select deptno from dept where dname like '%s%')
       group by deptno 

   select sum(sal),count(*) from emp group by deptno having deptno in(select deptno from dept where dname like '%s%')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!