Mysql 练习题10道(11-20题)

Posted 枸杞仙人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 练习题10道(11-20题)相关的知识,希望对你有一定的参考价值。

练习

取得最后入职的 5 名员工

日期也可以降序,升序。
select ename,hiredate from emp order by hiredate desc limit 5;

+--------+------------+
| ename  | hiredate   |
+--------+------------+
| ADAMS  | 1987-05-23 |
| SCOTT  | 1987-04-19 |
| MILLER | 1982-01-23 |
| FORD   | 1981-12-03 |
| JAMES  | 1981-12-03 |
+--------+------------+

取得每个薪水等级有多少员工

第一步:找出每个员工的薪水等级
select e.ename,s.grade from emp e join salgrade s where e.sal between s.losal and s.hisal;

+--------+-------+
| ename  | grade |
+--------+-------+
| SMITH  |     1 |
| ALLEN  |     3 |
| WARD   |     2 |
| JONES  |     4 |
| MARTIN |     2 |
| BLAKE  |     4 |
| CLARK  |     4 |
| SCOTT  |     4 |
| KING   |     5 |
| TURNER |     3 |
| ADAMS  |     1 |
| JAMES  |     1 |
| FORD   |     4 |
| MILLER |     2 |
+--------+-------+

第二步:根据薪水等级进行count
select s.grade,count(*) from emp join salgrade s on s.sal between s.losal and s.hisal group by s.grade;

+-------+----------+
| grade | count(*) |
+-------+----------+
|     1 |        3 |
|     3 |        2 |
|     2 |        3 |
|     4 |        5 |
|     5 |        1 |
+-------+----------+

列出所有员工及领导的姓名

select a.ename '员工',b.ename '领导' from emp a left join emp b on a.mgr = b.empno;

+--------+--------+
| 员工   | 领导   |
+--------+--------+
| SMITH  | FORD   |
| ALLEN  | BLAKE  |
| WARD   | BLAKE  |
| JONES  | KING   |
| MARTIN | BLAKE  |
| BLAKE  | KING   |
| CLARK  | KING   |
| SCOTT  | JONES  |
| KING   | NULL   |
| TURNER | BLAKE  |
| ADAMS  | SCOTT  |
| JAMES  | BLAKE  |
| FORD   | JONES  |
| MILLER | CLARK  |
+--------+--------+

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

select a.ename,a.empno,d.dname from emp a join emp b join dept d on a.hiredate<b.hiredate and a.mgr=b.empno and d.deptno=e.deptno;

+-------+-------+------------+
| ename | empno | dname      |
+-------+-------+------------+
| SMITH |  7369 | RESEARCH   |
| ALLEN |  7499 | SALES      |
| WARD  |  7521 | SALES      |
| JONES |  7566 | RESEARCH   |
| BLAKE |  7698 | SALES      |
| CLARK |  7782 | ACCOUNTING |
+-------+-------+------------+

列出部门名称和这些部门的员工信息, 同时列出那些没有员工的部门

select e.*,d.dname from emp e right join dept d on d.deptno=e.deptno;

+-------+--------+-----------+------+------------+---------+---------+--------+------------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO | dname      |
+-------+--------+-----------+------+------------+---------+---------+--------+------------+
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 | ACCOUNTING |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 | ACCOUNTING |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 | ACCOUNTING |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 | RESEARCH   |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |    NULL |     20 | RESEARCH   |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 |    NULL |     20 | RESEARCH   |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 | RESEARCH   |
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |    NULL |     20 | RESEARCH   |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 | SALES      |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 | SALES      |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 | SALES      |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 | SALES      |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 | SALES      |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 | SALES      |
|  NULL | NULL   | NULL      | NULL | NULL       |    NULL |    NULL |   NULL | OPERATIONS |
+-------+--------+-----------+------+------------+---------+---------+--------+------------+

列出至少有 5 个员工的所有部门

select deptno from emp group by deptno having count(*) >= 5;

+--------+
| deptno |
+--------+
|     20 |
|     30 |
+--------+

列出薪金比"SMITH" 多的所有员工信息

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.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 |    NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |    NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+

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

第一步:找出所有clerek
select ename,job from emp where job = 'clerk';

+--------+-------+
| ename  | job   |
+--------+-------+
| SMITH  | CLERK |
| ADAMS  | CLERK |
| JAMES  | CLERK |
| MILLER | CLERK |
+--------+-------+

第二步:加上部门名称
select e.ename,e.job,d.dname from emp e join dept d on e.deptno=d.deptno where e.job = 'clerk';

+--------+-------+------------+
| ename  | job   | dname      |
+--------+-------+------------+
| SMITH  | CLERK | RESEARCH   |
| ADAMS  | CLERK | RESEARCH   |
| JAMES  | CLERK | SALES      |
| MILLER | CLERK | ACCOUNTING |
+--------+-------+------------+

第三步:加上部门人数
select deptno,count(*) deptcount from emp group by deptno;

+--------+-----------+
| deptno | deptcount |
+--------+-----------+
|     20 |         5 |
|     30 |         6 |
|     10 |         3 |
+--------+-----------+

select e.ename,e.job,d.dname,t.deptcount from emp e join dept d join (select deptno,count(*) deptcount from emp group by deptno) t on e.deptno=d.deptno and e.deptno = t.deptno where e.job = 'clerk';

+--------+-------+------------+-----------+
| ename  | job   | dname      | deptcount |
+--------+-------+------------+-----------+
| SMITH  | CLERK | RESEARCH   |         5 |
| ADAMS  | CLERK | RESEARCH   |         5 |
| JAMES  | CLERK | SALES      |         6 |
| MILLER | CLERK | ACCOUNTING |         3 |
+--------+-------+------------+-----------+

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

select job,count(*) from emp group by job having min(sal) > 1500;

+-----------+----------+
| job       | count(*) |
+-----------+----------+
| MANAGER   |        3 |
| ANALYST   |        2 |
| PRESIDENT |        1 |
+-----------+----------+

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

select ename from emp where deptno = (select deptno from dept where dname='sales');

+--------+
| ename  |
+--------+
| ALLEN  |
| WARD   |
| MARTIN |
| BLAKE  |
| TURNER |
| JAMES  |
+--------+

以上是关于Mysql 练习题10道(11-20题)的主要内容,如果未能解决你的问题,请参考以下文章

oracle复杂查询的练习题

Mysql 练习题10道(1-10题)

mysql 查询练习题

MySQL之多表查询练习

MySQL之多表查询练习

018.查询练习50题(sql实例)