Mysql练习题13道(21-33题)

Posted 枸杞仙人

tags:

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

练习

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

select e.ename '员工',d.dname,t.ename '领导',s.grade from emp e join dept d on e.deptno=d.deptno left join emp t on e.mgr=t.empno join salgrade s on e.sal between s.losal and s.hisal where e.sal > (select avg(sal) from emp);

+--------+------------+--------+-------+
| 员工   | dname      | 领导   | grade |
+--------+------------+--------+-------+
| FORD   | RESEARCH   | JONES  |     4 |
| SCOTT  | RESEARCH   | JONES  |     4 |
| CLARK  | ACCOUNTING | KING   |     4 |
| BLAKE  | SALES      | KING   |     4 |
| JONES  | RESEARCH   | KING   |     4 |
| KING   | ACCOUNTING | NULL   |     5 |
+--------+------------+--------+-------+

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

select e.ename,d.dname,e.job from emp e join dept d on e.deptno = d.deptno where e.job = (select job from emp where ename='scott') and e.ename <> 'scott';

+-------+----------+---------+
| ename | dname    | job     |
+-------+----------+---------+
| FORD  | RESEARCH | ANALYST |
+-------+----------+---------+

列出薪金等于部门 30 中员工的薪金的其他员工的姓名和薪金

select e.ename,e.sal from emp e join (select distinct sal from emp where deptno = 30) t on e.sal = t.sal and e.deptno <> 30;

Empty set (0.00 sec)

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

select e.ename,e.sal,d.dname from emp e join dept d on e.deptno=d.deptno where e.sal > (select max(t.sal) from (select distinct sal from emp where deptno = 30) t )and e.deptno <> 30;

+-------+---------+------------+
| ename | sal     | dname      |
+-------+---------+------------+
| JONES | 2975.00 | RESEARCH   |
| SCOTT | 3000.00 | RESEARCH   |
| KING  | 5000.00 | ACCOUNTING |
| FORD  | 3000.00 | RESEARCH   |
+-------+---------+------------+

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

select d.deptno,count(e.ename) ecount ,ifnull(avg(e.sal),0) avgsal, ifnull(avg(timestampdiff(year,hiredate,now())),0) avgservicetime from emp e right join dept d on e.deptno=d.deptno group by d.deptno;

+--------+--------+-------------+----------------+
| deptno | ecount | avgsal      | avgservicetime |
+--------+--------+-------------+----------------+
|     10 |      3 | 2916.666667 |        39.0000 |
|     20 |      5 | 2175.000000 |        36.8000 |
|     30 |      6 | 1566.666667 |        39.0000 |
|     40 |      0 |    0.000000 |         0.0000 |
+--------+--------+-------------+----------------+

列出所有员工的姓名、部门名称和工资。

select e.ename,d.dname,e.sal from rmp e join dept d on e.deptno=e.deptno;

列出所有部门的详细信息和人数

select d.deptno,d.dname,d.loc,count(e.ename) from emp e right join dept d on e.deptno = d.deptno group by d.deptno,d.dname,d.loc;

+--------+------------+----------+----------------+
| deptno | dname      | loc      | count(e.ename) |
+--------+------------+----------+----------------+
|     10 | ACCOUNTING | NEW YORK |              3 |
|     20 | RESEARCH   | DALLAS   |              5 |
|     30 | SALES      | CHICAGO  |              6 |
|     40 | OPERATIONS | BOSTON   |              0 |
+--------+------------+----------+----------------+

列出各种工作的最低工资及从事此工作的雇员姓名

select e.ename,t.* from emp e join (select job,min(sal) minsal from emp group by job) t on e.job = t.job and e.sal = t.minsal;

+--------+-----------+---------+
| ename  | job       | minsal  |
+--------+-----------+---------+
| SMITH  | CLERK     |  800.00 |
| WARD   | SALESMAN  | 1250.00 |
| MARTIN | SALESMAN  | 1250.00 |
| CLARK  | MANAGER   | 2450.00 |
| SCOTT  | ANALYST   | 3000.00 |
| KING   | PRESIDENT | 5000.00 |
| FORD   | ANALYST   | 3000.00 |
+--------+-----------+---------+

列出各个部门的 MANAGER( 领导) 的最低薪金

select deptno, min(sal) from emp where job = 'MANAGER' group by deptno;

+--------+----------+
| deptno | min(sal) |
+--------+----------+
|     20 |  2975.00 |
|     30 |  2850.00 |
|     10 |  2450.00 |
+--------+----------+

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

select ename,sal*12 as yearsal from emp order by yearsal asc;

+--------+----------+
| ename  | yearsal  |
+--------+----------+
| SMITH  |  9600.00 |
| JAMES  | 11400.00 |
| ADAMS  | 13200.00 |
| WARD   | 15000.00 |
| MARTIN | 15000.00 |
| MILLER | 15600.00 |
| TURNER | 18000.00 |
| ALLEN  | 19200.00 |
| CLARK  | 29400.00 |
| BLAKE  | 34200.00 |
| JONES  | 35700.00 |
| SCOTT  | 36000.00 |
| FORD   | 36000.00 |
| KING   | 60000.00 |
+--------+----------+

求出员工领导的薪水超过3000的员工名称与领导

select a.ename '员工',b.ename '领导' from emp a join emp b on a.mgr = b.empno where b.sal > 3000;

+--------+--------+
| 员工   | 领导   |
+--------+--------+
| JONES  | KING   |
| BLAKE  | KING   |
| CLARK  | KING   |
+--------+--------+

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

select d.deptno,d.dname,d.loc,count(e.ename),ifnull(sum(e.sal),0) sumsal 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) | sumsal   |
+--------+------------+---------+----------------+----------+
|     20 | RESEARCH   | DALLAS  |              5 | 10875.00 |
|     30 | SALES      | CHICAGO |              6 |  9400.00 |
|     40 | OPERATIONS | BOSTON  |              0 |     0.00 |
+--------+------------+---------+----------------+----------+

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

update emp set sal = sal * 1.1 where timestampdiff(YEAR, hiredate, now()) > 30;

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

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

sql 查询语句的练习2

Oracle的sql语句上级练习和答案

Oracle的sql语句上机练习和答案

sql 语句 在所有部门中查询薪金最高或最低的雇员的信息

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