• MySQL练习题


    练习题

    (1)、取得每个部门最高薪水的人员名称

    第一步: 取每个部门最高薪水

    mysql> select deptno,max(sal) from EMP group by deptno;
    +--------+----------+
    | deptno | max(sal) |
    +--------+----------+
    |     10 |     5000 |
    |     20 |     3000 |
    |     30 |     2850 |
    +--------+----------+
    3 rows in set (0.00 sec)
    

    第二步:将以上的结果当做临时表t,t表盒emp_e表进行连接,条件是:t.deptno = e.deptno and t.maxsal axsal = e.sal

    select 
          e.ename,t.* 
    from 
          (select deptno,max(sal)as maxsal from EMP group by deptno) t 
    join 
          EMP e  
    on 
          t.deptno = e.deptnno and t.maxsal = e.sal;
    
    mysql> select e.ename,t.* from (select deptno,max(sal)as maxsal from EMP group by deptno) t join EMP e  on t.deptno = e.deptnno and t.maxsal = e.sal;
    +-------+--------+--------+
    | ename | deptno | maxsal |
    +-------+--------+--------+
    | BLAKE |     30 |   2850 |
    | SCOTT |     20 |   3000 |
    | KING  |     10 |   5000 |
    | FORD  |     20 |   3000 |
    +-------+--------+--------+
    4 rows in set (0.00 sec)
    
    

    (2)、那些人的薪水在部门的平均薪水之上

    第一步:找出每个部门的平均薪水

    mysql> select avg(sal),deptno from EMP group by deptno;
    +--------------------+--------+
    | avg(sal)           | deptno |
    +--------------------+--------+
    | 2916.6666666666665 |     10 |
    |               2175 |     20 |
    | 1566.6666666666667 |     30 |
    +--------------------+--------+
    3 rows in set (0.00 sec)
    

    第二步:将以上查询结果当做t表,t表和emp表连接
    条件:部门编号相同,并且emp的sal大与t表的avgsal

    mysql> select t.*,e.ename,e.sal
        -> from EMP e
        -> join (select avg(sal) as avgsal,deptno from EMP group by deptno) t
        -> on e.deptno = t.deptno and e.sal > t.avgsal;
    +--------------------+--------+-------+------+
    | avgsal             | deptno | ename | sal  |
    +--------------------+--------+-------+------+
    | 1566.6666666666667 |     30 | ALLEN | 1600 |
    |               2175 |     20 | JONES | 2975 |
    | 1566.6666666666667 |     30 | BLAKE | 2850 |
    |               2175 |     20 | SCOTT | 3000 |
    | 2916.6666666666665 |     10 | KING  | 5000 |
    |               2175 |     20 | FORD  | 3000 |
    +--------------------+--------+-------+------+
    6 rows in set (0.00 sec)
    

    (3)、取得部门中(所有人)平均的薪水等级

    平均的薪水等级:先计算每一个薪水的等级,然后找出薪水等级的平均数
    平均薪水的等级:先计算平均薪水,然后找出每个平均薪水的等级值

    第一步:找出每个人的薪水等级
    emp e salgrade s表连接
    连接条件: e.sal between s.losal and s.hisal

    mysql> select e.ename,e.sal,e.deptno,s.grade
        -> from EMP e
        -> join SALGRADE s
        -> on e.sal between s.losal and s.hisal;
    +--------+------+--------+-------+
    | ename  | sal  | deptno | grade |
    +--------+------+--------+-------+
    | SMITH  |  800 |     20 |     1 |
    | ALLEN  | 1600 |     30 |     3 |
    | WARD   | 1250 |     30 |     2 |
    | JONES  | 2975 |     20 |     4 |
    | MARTIN | 1250 |     30 |     2 |
    | BLAKE  | 2850 |     30 |     4 |
    | CLARK  | 2450 |     10 |     4 |
    | SCOTT  | 3000 |     20 |     4 |
    | KING   | 5000 |     10 |     5 |
    | TURNER | 1500 |     30 |     3 |
    | ADAMS  | 1100 |     20 |     1 |
    | JAMES  |  950 |     30 |     1 |
    | FORD   | 3000 |     20 |     4 |
    | MILLER | 1300 |     10 |     2 |
    +--------+------+--------+-------+
    14 rows in set (0.00 sec)
    
    第二步:基于以上的结果继续按照deptno分组,求grade的平均值
     select e.deptno,avg(s.grade) 
        -> from EMP e
        -> join SALGRADE s
        -> on e.sal between s.losal and s.hisal
        -> group by e.deptno;
    +--------+--------------+
    | deptno | avg(s.grade) |
    +--------+--------------+
    |     10 |       3.6667 |
    |     20 |       2.8000 |
    |     30 |       2.5000 |
    +--------+--------------+
    3 rows in set (0.00 sec)
    
    

    (4)、不准用组函数(MAX), 取得最高薪资

    第一种:sal 降序,limit 1

    mysql> select * from EMP order by sal desc limit 1;
    +-------+-------+-----------+------+------------+------+------+--------+
    | EMPNO | ENAME | JOB       | MGR  | HIREDATE   | SAL  | COMM | DEPTNO |
    +-------+-------+-----------+------+------------+------+------+--------+
    |  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
    +-------+-------+-----------+------+------------+------+------+--------+
    1 row in set (0.00 sec)
    

    第二种:select max(sal) from emp;

    第三种:表的自连接

    
    mysql> select sal from EMP where sal not in(select distinct a.sal from EMP a join EMP b on a.sal < b.sal);
    +------+
    | sal  |
    +------+
    | 5000 |
    +------+
    1 row in set (0.01 sec)
    

    (5)、取得平均薪水最高的部门的部门编号

    第一种方案:降序选第一个
    第一步:找出每个部门的平均薪水

    select deptno,avg(sal) as avgsal from emp group by deptno;
    +--------+--------------------+
    | deptno | avgsal             |
    +--------+--------------------+
    |     10 | 2916.6666666666665 |
    |     20 |               2175 |
    |     30 | 1566.6666666666667 |
    +--------+--------------------+
    3 rows in set (0.00 sec)
    

    第二步:降序选第一个

    mysql> select deptno,avg(sal) as avgsal from EMP group by deptno order by avgsal desc limit 1;
    +--------+--------------------+
    | deptno | avgsal             |
    +--------+--------------------+
    |     10 | 2916.6666666666665 |
    +--------+--------------------+
    1 row in set (0.00 sec)
    

    (6)、取得平均薪水最高的部门的部门名称

    mysql> select deptno,avg(sal) as avgsal from EMP e group by deptno order by avgsal desc  limit 1;
    +--------+--------------------+
    | deptno | avgsal             |
    +--------+--------------------+
    |     10 | 2916.6666666666665 |
    +--------+--------------------+
    1 row in set (0.01 sec)
    

    (7)、求平均薪水的等级最低的部门的部门名称

    先查出最每个部门平均的薪水

    mysql> select deptno,avg(sal) from EMP group by deptno;
    +--------+--------------------+
    | deptno | avg(sal)           |
    +--------+--------------------+
    |     10 | 2916.6666666666665 |
    |     20 |               2175 |
    |     30 | 1566.6666666666667 |
    +--------+--------------------+
    3 rows in set (0.00 sec)
    

    找出每个部门的平均薪水的等级 然后在和salgrade表连接 :条件t.avgsal between s.losal and s.hisal

    mysql> select t.*,s.grade 
        -> from 
        ->   (select dname,avg(sal)  avgsal from EMP e join DEPT d on e.deptno=d.deptno group by d.dname) t 
        -> join 
        ->   SALGRADE s 
        -> on  
        ->   t.avgsal between s.losal and s.hisal ;
    +------------+--------------------+-------+
    | dname      | avgsal             | grade |
    +------------+--------------------+-------+
    | ACCOUNTING | 2916.6666666666665 |     4 |
    | RESEARCH   |               2175 |     4 |
    | SALES      | 1566.6666666666667 |     3 |
    +------------+--------------------+-------+
    3 rows in set (0.00 sec)
    

    找出平均薪水最低的对应的等级一定是最低的

    mysql> select avg(sal) as avgsal from EMP group by deptno order by avgsal asc limit 1 ;
    +--------------------+
    | avgsal             |
    +--------------------+
    | 1566.6666666666667 |
    +--------------------+
    1 row in set (0.00 sec)
    

    结果

    mysql>  select t.*,s.grade  from                                                                                            
        -> (select dname,avg(sal)  avgsal from EMP e join DEPT d on e.deptno=d.deptno group by d.dname) t  
        -> join        
        -> SALGRADE s  on         
        -> t.avgsal between s.losal and s.hisal 
        -> where 
        -> s.grade=(select grade from SALGRADE where(select avg(sal) as avgsal from EMP group by deptno order by avgsal asc limit 1 )between losal and hisal)
        -> ;
    +-------+--------------------+-------+
    | dname | avgsal             | grade |
    +-------+--------------------+-------+
    | SALES | 1566.6666666666667 |     3 |
    +-------+--------------------+-------+
    1 row in set (0.00 sec)
    

    (8)、取得比普通员工(员工代码没有在mgr字段上出现的) 的最高薪水还要高的领导人姓名

    第一步:找出普通员工的最高薪水
    not in 在使用的时候,需要手动排除 null值

    mysql> select max(sal) from EMP where empno not in(select distinct mgr from EMP where mgr is not null);
    +----------+
    | max(sal) |
    +----------+
    |     1600 |
    +----------+
    1 row in set (0.00 sec)
    

    第二步找出高于1600的工资

    mysql> select ename,sal from EMP where sal >(select max(sal) from EMP where empno not in(select distinct mgr from EMP where mgr is not null))
        -> ;
    +-------+------+
    | ename | sal  |
    +-------+------+
    | JONES | 2975 |
    | BLAKE | 2850 |
    | CLARK | 2450 |
    | SCOTT | 3000 |
    | KING  | 5000 |
    | FORD  | 3000 |
    +-------+------+
    6 rows in set (0.00 sec)
    

    (9)、取得薪水最高的前五名员工

    mysql> select ename,sal from EMP order by sal desc limit 5;
    +-------+------+
    | ename | sal  |
    +-------+------+
    | KING  | 5000 |
    | FORD  | 3000 |
    | SCOTT | 3000 |
    | JONES | 2975 |
    | BLAKE | 2850 |
    +-------+------+
    5 rows in set (0.00 sec)
    

    (10)、取得薪水最高的六到第十名

    mysql> select ename,sal from EMP  order by sal desc limit 5,5;
    +--------+------+
    | ename  | sal  |
    +--------+------+
    | CLARK  | 2450 |
    | ALLEN  | 1600 |
    | TURNER | 1500 |
    | MILLER | 1300 |
    | WARD   | 1250 |
    +--------+------+
    5 rows in set (0.00 sec)
    

    (11)、取得最后入职的5名员工

    mysql> select * from EMP order by HIREDATE desc limit 5 ;
    +-------+--------+---------+------+------------+------+------+--------+
    | EMPNO | ENAME  | JOB     | MGR  | HIREDATE   | SAL  | COMM | DEPTNO |
    +-------+--------+---------+------+------------+------+------+--------+
    |  7788 | SCOTT  | ANALYST | 7566 | 1987-07-13 | 3000 | NULL |     20 |
    |  7876 | ADAMS  | CLERK   | 7788 | 1987-07-13 | 1100 | NULL |     20 |
    |  7934 | MILLER | CLERK   | 7782 | 1982-01-23 | 1300 | NULL |     10 |
    |  7900 | JAMES  | CLERK   | 7698 | 1981-12-03 |  950 | NULL |     30 |
    |  7902 | FORD   | ANALYST | 7566 | 1981-12-03 | 3000 | NULL |     20 |
    +-------+--------+---------+------+------------+------+------+--------+
    5 rows in set (0.00 sec)
    

    (12)、取得每个薪水 等级有多少个员工

    第一步:找出每个员工的薪水等级

    mysql> select e.ename,e.sal,s.grade from EMP e join SALGRADE s on e.sal between s.losal and s.hisal;
    +--------+------+-------+
    | ename  | sal  | grade |
    +--------+------+-------+
    | SMITH  |  800 |     1 |
    | ALLEN  | 1600 |     3 |
    | WARD   | 1250 |     2 |
    | JONES  | 2975 |     4 |
    | MARTIN | 1250 |     2 |
    | BLAKE  | 2850 |     4 |
    | CLARK  | 2450 |     4 |
    | SCOTT  | 3000 |     4 |
    | KING   | 5000 |     5 |
    | TURNER | 1500 |     3 |
    | ADAMS  | 1100 |     1 |
    | JAMES  |  950 |     1 |
    | FORD   | 3000 |     4 |
    | MILLER | 1300 |     2 |
    +--------+------+-------+
    14 rows in set (0.00 sec)
    

    结果

    mysql> select s.grade,count(*) from EMP e join SALGRADE s on e.sal between s.losal and s.hisal group by s.grade;
    +-------+----------+
    | grade | count(*) |
    +-------+----------+
    |     1 |        3 |
    |     2 |        3 |
    |     3 |        2 |
    |     4 |        5 |
    |     5 |        1 |
    +-------+----------+
    5 rows in set (0.00 sec)
    

    (13)、取得

    (14)、列出所有员工及领导的姓名

    mysql> select a.ename "员工",b.ename "领导" from EMP a join EMP b on a.mgr=b.empno;
    +--------+--------+
    | 员工   | 领导   |
    +--------+--------+
    | SMITH  | FORD   |
    | ALLEN  | BLAKE  |
    | WARD   | BLAKE  |
    | JONES  | KING   |
    | MARTIN | BLAKE  |
    | BLAKE  | KING   |
    | CLARK  | KING   |
    | SCOTT  | JONES  |
    | TURNER | BLAKE  |
    | ADAMS  | SCOTT  |
    | JAMES  | BLAKE  |
    | FORD   | JONES  |
    | MILLER | CLARK  |
    +--------+--------+
    13 rows in set (0.00 sec)
    

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

    emp a  员工表
    emp b  领导表
    a.mgr = b.empno and a.hiredate < b.hiredate
    select 
         a.ename "员工",b.ename "领导",b.hiredate,d.dname
    from 
         EMP a
    join
         EMP b
    on 
         a.mgr = b.empno 
    join 
         DEPT d
    on 
          a.deptno = d.deptno
    and a.hiredate < b.hiredate
    +--------+--------+------------+------------+
    | 员工   | 领导   | hiredate   | dname      |
    +--------+--------+------------+------------+
    | CLARK  | KING   | 1981-11-17 | ACCOUNTING |
    | SMITH  | FORD   | 1981-12-03 | RESEARCH   |
    | JONES  | KING   | 1981-11-17 | RESEARCH   |
    | ALLEN  | BLAKE  | 1981-05-01 | SALES      |
    | WARD   | BLAKE  | 1981-05-01 | SALES      |
    | BLAKE  | KING   | 1981-11-17 | SALES      |
    +--------+--------+------------+------------+
    6 rows in set (0.00 sec)
    

    (16)、列出部门名称和这些部门的员工信息,同时列出哪些没有员工的部门

    emp e
    dept d
    
    select 
          e.*,d.dname
    from
          EMP e
    right join
          DEPT d
    on
          e.deptno=d.deptno
    +-------+--------+-----------+------+------------+------+------+--------+------------+
    | EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL  | COMM | DEPTNO | dname      |
    +-------+--------+-----------+------+------------+------+------+--------+------------+
    |  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 | ACCOUNTING |
    |  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 | ACCOUNTING |
    |  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300 | NULL |     10 | ACCOUNTING |
    |  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 | RESEARCH   |
    |  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 | RESEARCH   |
    |  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-13 | 3000 | NULL |     20 | RESEARCH   |
    |  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 | RESEARCH   |
    |  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 | RESEARCH   |
    |  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 | SALES      |
    |  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 | SALES      |
    |  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 | SALES      |
    |  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 | SALES      |
    |  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 | SALES      |
    |  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 | SALES      |
    |  NULL | NULL   | NULL      | NULL | NULL       | NULL | NULL |   NULL | OPERATIONS |
    +-------+--------+-----------+------+------------+------+------+--------+------------+
    15 rows in set (0.00 sec)
    

    (17)、列出至少有五个员工的所有部门

    mysql> select deptno,count(deptno) from EMP group by deptno having count(deptno)>=5;
    +--------+---------------+
    | deptno | count(deptno) |
    +--------+---------------+
    |     20 |             5 |
    |     30 |             6 |
    +--------+---------------+
    2 rows in set (0.00 sec)
    

    (18)、列出薪金比"SMITH"多的所有员工信息

    mysql>  select * from EMP where sal >(select sal from EMP where ename="SMITH");
    +-------+--------+-----------+------+------------+------+------+--------+
    | EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL  | COMM | DEPTNO |
    +-------+--------+-----------+------+------------+------+------+--------+
    |  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
    |  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 |
    |  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
    |  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
    |  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
    |  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
    |  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-13 | 3000 | NULL |     20 |
    |  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
    |  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
    |  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
    |  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
    |  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
    |  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300 | NULL |     10 |
    +-------+--------+-----------+------+------------+------+------+--------+
    13 rows in set (0.00 sec)
    

    (19)、列出所有"CLERK"(办事员)的姓名及其姓名,部门的人数

    mysql> 
    select 
          e.ename,e.job 
    from 
          EMP e
    join      
          DEPT d
    on 
          e.deptno = d.deptno
    where 
          e.JOB ="CLERK";
    

    找出部门人数

    mysql> select deptno,count(*)as dptcount from EMP group by deptno;
    +--------+----------+
    | deptno | dptcount |
    +--------+----------+
    |     10 |        3 |
    |     20 |        5 |
    |     30 |        6 |
    +--------+----------+
    3 rows in set (0.00 sec)
    ```sql
    select 
    t1.*,t2.deptcount
    from 
          (select 
                e.ename,e.job,d.dname,d.deptno
           from 
                EMP e
          join      
                DEPT d
          on 
                e.deptno = d.deptno
          where 
                e.JOB ="CLERK") t1
    join 
          (select 
                deptno,count(*)as deptcount 
          from 
                EMP 
          group by 
                deptno) t2
    on t1.deptno = t2.deptno
    +--------+-------+------------+--------+-----------+
    | ename  | job   | dname      | deptno | deptcount |
    +--------+-------+------------+--------+-----------+
    | SMITH  | CLERK | RESEARCH   |     20 |         5 |
    | ADAMS  | CLERK | RESEARCH   |     20 |         5 |
    | JAMES  | CLERK | SALES      |     30 |         6 |
    | MILLER | CLERK | ACCOUNTING |     10 |         3 |
    +--------+-------+------------+--------+-----------+
    
    

    (20)、列出最低薪资大与1500的各种工作及从事此工作的全部雇员人数

    按照工作岗位分组求最小值

    mysql> select job,count(*) from EMP group by job having min(sal) >1500;
    +-----------+----------+
    | job       | count(*) |
    +-----------+----------+
    | ANALYST   |        2 |
    | MANAGER   |        3 |
    | PRESIDENT |        1 |
    +-----------+----------+
    3 rows in set (0.00 sec)
    

    (21)、列出部门"SALES"<销售部> 工作的员工的姓名,假定不知道销售部的部门编号

    mysql> select ename from EMP where deptno =(select deptno from DEPT where dname = "SALES");
    +--------+
    | ename  |
    +--------+
    | ALLEN  |
    | WARD   |
    | MARTIN |
    | BLAKE  |
    | TURNER |
    | JAMES  |
    +--------+
    

    (22)、列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,雇员的工资等级

    mysql> select e.ename "员工",d.dname,a.ename "领导",s.grade
        -> from EMP e
        -> join DEPT d
        -> on e.deptno = d.deptno
        -> left join EMP a
        -> on e.mgr = a.empno
        -> join SALGRADE s
        -> on e.sal between s.losal and s.hisal
        -> where e.sal >(select avg(sal) from EMP);
    +--------+------------+--------+-------+
    | 员工   | dname      | 领导   | grade |
    +--------+------------+--------+-------+
    | JONES  | RESEARCH   | KING   |     4 |
    | BLAKE  | SALES      | KING   |     4 |
    | CLARK  | ACCOUNTING | KING   |     4 |
    | SCOTT  | RESEARCH   | JONES  |     4 |
    | KING   | ACCOUNTING | NULL   |     5 |
    | FORD   | RESEARCH   | JONES  |     4 |
    +--------+------------+--------+-------+
    6 rows in set (0.00 sec)
    
    

    (23)、列出与"SCOTT" 从事相同工作的所有员工及部门名称

    mysql> select  e.ename,e.job,d.dname from EMP e join DEPT d on e.deptno = d.deptno where  e.job=(select job from EMP where enname="SCOTT") and e.ename <>"SCOTT";
    +-------+---------+----------+
    | ename | job     | dname    |
    +-------+---------+----------+
    | FORD  | ANALYST | RESEARCH |
    +-------+---------+----------+
    1 row in set (0.00 sec)
    

    (24)、列出薪金等于部门30中员工的薪金的其他员工的姓名的薪金

    mysql> select ename,sal from EMP where sal in (select sal from EMP e where deptno = 30) and deptno <> 30;
    

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

    mysql> select max(sal) from EMP where deptno =30;
    +----------+
    | max(sal) |
    +----------+
    |     2850 |
    +----------+
    1 row in set (0.00 sec)
    mysql> select  e.ename ,e.sal ,d.dname from  EMP e join DEPT d on e.deptno=d.deptno where sal >(select max(sal) from EMP where deptno =30);
    +-------+------+------------+
    | ename | sal  | dname      |
    +-------+------+------------+
    | JONES | 2975 | RESEARCH   |
    | SCOTT | 3000 | RESEARCH   |
    | KING  | 5000 | ACCOUNTING |
    | FORD  | 3000 | RESEARCH   |
    +-------+------+------------+
    4 rows in set (0.00 sec)
    

    (26)、列出在每个部门工作的员工数量,,平均工资和平均服务期限

    mysql> select d.*,count(e.ename) from EMP e right join DEPT d on d.deptno = e.deptno group by d.deptno;
    +--------+------------+----------+----------------+
    | DEPTNO | DNAME      | LOC      | count(e.ename) |
    +--------+------------+----------+----------------+
    |     10 | ACCOUNTING | NEW YORK |              3 |
    |     20 | RESEARCH   | DALLAS   |              5 |
    |     30 | SALES      | CHICAGO  |              6 |
    |     40 | OPERATIONS | BOSTON   |              0 |
    +--------+------------+----------+----------------+
    4 rows in set (0.00 sec)
    
    mysql> select d.*,count(e.ename),avg(sal),avg(timestampdiff(YEAR,hiredate,now())) as time from EMP e right join DEPT d on d.deptno = e.deptno group by d.deptno;
    +--------+------------+----------+----------------+--------------------+---------+
    | DEPTNO | DNAME      | LOC      | count(e.ename) | avg(sal)           | time    |
    +--------+------------+----------+----------------+--------------------+---------+
    |     10 | ACCOUNTING | NEW YORK |              3 | 2916.6666666666665 | 38.3333 |
    |     20 | RESEARCH   | DALLAS   |              5 |               2175 | 36.4000 |
    |     30 | SALES      | CHICAGO  |              6 | 1566.6666666666667 | 38.5000 |
    |     40 | OPERATIONS | BOSTON   |              0 |               NULL |    NULL |
    +--------+------------+----------+----------------+--------------------+---------+
    4 rows in set (0.00 sec)
    

    (27)、列出所有员工的姓名、部门名称和工资

    mysql> select e.ename,d.dname,e.sal from EMP e join DEPT d on e.deptno=d.deptno
        -> ;
    +--------+------------+------+
    | ename  | dname      | sal  |
    +--------+------------+------+
    | CLARK  | ACCOUNTING | 2450 |
    | KING   | ACCOUNTING | 5000 |
    | MILLER | ACCOUNTING | 1300 |
    | SMITH  | RESEARCH   |  800 |
    | JONES  | RESEARCH   | 2975 |
    | SCOTT  | RESEARCH   | 3000 |
    | ADAMS  | RESEARCH   | 1100 |
    | FORD   | RESEARCH   | 3000 |
    | ALLEN  | SALES      | 1600 |
    | WARD   | SALES      | 1250 |
    | MARTIN | SALES      | 1250 |
    | BLAKE  | SALES      | 2850 |
    | TURNER | SALES      | 1500 |
    | JAMES  | SALES      |  950 |
    +--------+------------+------+
    14 rows in set (0.00 sec)
    

    (28)、列出所有员工的详细信息和人数

    mysql> select d.deptno ,d.dname,d.loc from EMP e join DEPT d on e.deptno = d.deptno group by d.deptno ,d.dname,d.loc;
    +--------+------------+----------+
    | deptno | dname      | loc      |
    +--------+------------+----------+
    |     10 | ACCOUNTING | NEW YORK |
    |     20 | RESEARCH   | DALLAS   |
    |     30 | SALES      | CHICAGO  |
    +--------+------------+----------+
    3 rows in set (0.00 sec)
    

    (29)、列出各种工作的最低工资及其从事此工作的雇员姓名

    mysql> select e.ename,t.* from EMP e join (select job,min(sal) as sal from EMP group by job) t on e.job=t.job and e.sal = t.sal;
    +--------+-----------+------+
    | ename  | job       | sal  |
    +--------+-----------+------+
    | SMITH  | CLERK     |  800 |
    | WARD   | SALESMAN  | 1250 |
    | MARTIN | SALESMAN  | 1250 |
    | CLARK  | MANAGER   | 2450 |
    | SCOTT  | ANALYST   | 3000 |
    | KING   | PRESIDENT | 5000 |
    | FORD   | ANALYST   | 3000 |
    +--------+-----------+------+
    7 rows in set (0.00 sec)
    

    (30)、列出各个部门的MANAGER()领导的最低工资

    mysql> select deptno,min(sal) from EMP where job='MANAGER' group by deptno;
    +--------+----------+
    | deptno | min(sal) |
    +--------+----------+
    |     10 |     2450 |
    |     20 |     2975 |
    |     30 |     2850 |
    +--------+----------+
    3 rows in set (0.00 sec)
    

    (31)、列出所有员工的年工资,按年薪从低到高排序

    mysql> select ename,(sal+ ifnull(comm,0))*12 as yearsal from EMP order by yearsal asc;
    +--------+---------+
    | ename  | yearsal |
    +--------+---------+
    | SMITH  |    9600 |
    | JAMES  |   11400 |
    | ADAMS  |   13200 |
    | MILLER |   15600 |
    | TURNER |   18000 |
    | WARD   |   21000 |
    | ALLEN  |   22800 |
    | CLARK  |   29400 |
    | MARTIN |   31800 |
    | BLAKE  |   34200 |
    | JONES  |   35700 |
    | SCOTT  |   36000 |
    | FORD   |   36000 |
    | KING   |   60000 |
    +--------+---------+
    14 rows in set (0.00 sec)
    
    

    (32)、求出员工领导的薪水超过3000 的员工名称与领导

    mysql> select  a.ename "员工",a.sal,b.ename "领导",b.sal from EMP a join EMP  b on a.mgr=b.empno where b.sal >3000;
    +--------+------+--------+------+
    | 员工   | sal  | 领导   | sal  |
    +--------+------+--------+------+
    | JONES  | 2975 | KING   | 5000 |
    | BLAKE  | 2850 | KING   | 5000 |
    | CLARK  | 2450 | KING   | 5000 |
    +--------+------+--------+------+
    3 rows in set (0.00 sec)
    
    

    (33)、求出部门名称中,带's'字符的部门员工的工资合计、部门人数,

    mysql> select d.deptno,d.dname,d.loc,count(e.ename),ifnull(sum(e.sal),0) from EMP e right join DEPT d on e.deptno = d.deptno  where d.dname like '%s%' group by d.deptno,d.dname,d.loc;
    +--------+------------+---------+----------------+----------------------+
    | deptno | dname      | loc     | count(e.ename) | ifnull(sum(e.sal),0) |
    +--------+------------+---------+----------------+----------------------+
    |     20 | RESEARCH   | DALLAS  |              5 |                10875 |
    |     30 | SALES      | CHICAGO |              6 |                 9400 |
    |     40 | OPERATIONS | BOSTON  |              0 |                    0 |
    +--------+------------+---------+----------------+----------------------+
    

    (32)、给任职日期超过30年的员工加薪10%

    update EMP set sal=sal * 1.1 where timestampdiff(YEAR,hiredate,now())>30;
  • 相关阅读:
    nginx 域名跳转一例~~~(rewrite、proxy)
    军哥 LNMP 常见问题
    Zabbix安装
    PHP远程连接mysql
    Zend Studio 下载
    军哥LNMP优化
    网络流量监控分析工具 Ntopng 安装
    Linux VPS禁止某个IP访问
    DDoS deflate
    MySQL数据库的优化
  • 原文地址:https://www.cnblogs.com/rzkwz/p/13296830.html
Copyright © 2020-2023  润新知