讲义和SQL脚本地址:https://github.com/dgfwork/MySQL_Notebook
1.取得每个部门最高薪水的人员名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.deptno,max(e.sal) as maxsal 3 from 4 emp e 5 group by 6 e.deptno 7 8 9 -- mysql> select 10 -- -> e.deptno,max(e.sal) 11 -- -> from 12 -- -> emp e 13 -- -> group by 14 -- -> e.deptno; 15 -- +--------+------------+ 16 -- | deptno | max(e.sal) | 17 -- +--------+------------+ 18 -- | 10 | 5000.00 | 19 -- | 20 | 3000.00 | 20 -- | 30 | 2850.00 | 21 -- +--------+------------+ 22 23 -- # 将以上查询结果当做一个临时表t(deptno,maxsal) 24 select 25 e.deptno, 26 e.ename, 27 t.maxsal, 28 e.sal 29 from 30 (select 31 e.deptno,max(e.sal) as maxsal 32 from 33 emp e 34 group by 35 e.deptno) t 36 37 join 38 emp e 39 on 40 e.deptno=t.deptno 41 where 42 t.maxsal=e.sal 43 44 -- mysql> select 45 -- -> e.deptno, 46 -- -> e.ename, 47 -- -> t.maxsal, 48 -- -> e.sal 49 -- -> from 50 -- -> (select 51 -- -> e.deptno,max(e.sal) as maxsal 52 -- -> from 53 -- -> emp e 54 -- -> group by 55 -- -> e.deptno) t 56 -- -> join 57 -- -> emp e 58 -- -> on 59 -- -> e.deptno=t.deptno 60 -- -> where 61 -- -> t.maxsal=e.sal; 62 -- +--------+-------+---------+---------+ 63 -- | deptno | ename | maxsal | sal | 64 -- +--------+-------+---------+---------+ 65 -- | 30 | BLAKE | 2850.00 | 2850.00 | 66 -- | 20 | SCOTT | 3000.00 | 3000.00 | 67 -- | 10 | KING | 5000.00 | 5000.00 | 68 -- | 20 | FORD | 3000.00 | 3000.00 | 69 -- +--------+-------+---------+---------+
2.那些人的薪水在部门平均薪水之上
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- 解析:先查出每个部门的平均薪水表,再将其当做临时表 2 t(deptno,avgsal) 3 select 4 deptno, 5 avg(sal) as avgsal 6 from 7 emp e 8 group by 9 deptno 10 11 -- mysql> select 12 -- -> deptno, 13 -- -> avg(sal) as avgsal 14 -- -> from 15 -- -> emp e 16 -- -> group by 17 -- -> deptno; 18 -- +--------+-------------+ 19 -- | deptno | avgsal | 20 -- +--------+-------------+ 21 -- | 10 | 3150.000000 | 22 -- | 20 | 2220.833333 | 23 -- | 30 | 1566.666667 | 24 -- +--------+-------------+ 25 26 27 select 28 e.deptno, 29 e.ename, 30 e.sal 31 from 32 (select 33 deptno, 34 avg(sal) as avgsal 35 from 36 emp e 37 group by 38 deptno) t 39 join 40 emp e 41 on 42 t.deptno=e.deptno 43 where 44 e.sal>t.avgsal; 45 46 -- mysql> select 47 -- -> e.deptno, 48 -- -> e.ename, 49 -- -> e.sal 50 -- -> from 51 -- -> (select 52 -- -> deptno, 53 -- -> avg(sal) as avgsal 54 -- -> from 55 -- -> emp e 56 -- -> group by 57 -- -> deptno) t 58 -- -> join 59 -- -> emp e 60 -- -> on 61 -- -> t.deptno=e.deptno 62 -- -> where 63 -- -> e.sal>t.avgsal; 64 -- +--------+-------+---------+ 65 -- | deptno | ename | sal | 66 -- +--------+-------+---------+ 67 -- | 30 | ALLEN | 1600.00 | 68 -- | 20 | JONES | 2975.00 | 69 -- | 30 | BLAKE | 2850.00 | 70 -- | 20 | CLARK | 2450.00 | 71 -- | 20 | SCOTT | 3000.00 | 72 -- | 10 | KING | 5000.00 | 73 -- | 20 | FORD | 3000.00 | 74 -- +--------+-------+---------+
3.取得部门中(所有人的)平均薪水等级
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- 3.1平均薪水的等级 2 -- 第一步:先找平均薪水,生成一张临时表 3 -- t(deptno,avgsal) 4 5 select 6 deptno, 7 avg(sal) as avgsal 8 from 9 emp e 10 group by 11 deptno 12 -- 第二步:根据上面的结果和薪水等级表连接 13 select 14 t.deptno, 15 s.grade 16 from 17 (select 18 deptno, 19 avg(sal) as avgsal 20 from 21 emp e 22 group by 23 deptno) t 24 join 25 salgrade s 26 on 27 t.avgsal between s.losal and s.hisal 28 order by deptno; 29 30 -- 结果如下: 31 -- mysql> select 32 -- -> t.deptno, 33 -- -> s.grade 34 -- -> from 35 -- -> (select 36 -- -> deptno, 37 -- -> avg(sal) as avgsal 38 -- -> from 39 -- -> emp e 40 -- -> group by 41 -- -> deptno) t 42 -- -> join 43 -- -> salgrade s 44 -- -> on 45 -- -> t.avgsal between s.losal and s.hisal 46 -- -> ; 47 -- +--------+-------+ 48 -- | deptno | grade | 49 -- +--------+-------+ 50 -- | 30 | 3 | 51 -- | 20 | 4 | 52 -- | 10 | 5 | 53 -- +--------+-------+ 54 55 56 57 -- 3.2每个部门所有人平均的薪水等级 58 select 59 e.ename, 60 e.sal, 61 s.grade 62 from 63 emp e 64 join 65 salgrade s 66 on 67 e.sal between s.losal and s.hisal; 68 69 70 select 71 t.deptno, 72 avg(grade) as avg_grade 73 from 74 (select 75 e.deptno, 76 e.ename, 77 e.sal, 78 s.grade 79 from 80 emp e 81 join 82 salgrade s 83 on 84 e.sal between s.losal and s.hisal) t 85 group by 86 t.deptno; 87 -- 结果如下: 88 -- mysql> select 89 -- -> t.deptno, 90 -- -> avg(grade) as avg_grade 91 -- -> from 92 -- -> (select 93 -- -> e.deptno, 94 -- -> e.ename, 95 -- -> e.sal, 96 -- -> s.grade 97 -- -> from 98 -- -> emp e 99 -- -> join 100 -- -> salgrade s 101 -- -> on 102 -- -> e.sal between s.losal and s.hisal) t 103 -- -> group by 104 -- -> t.deptno; 105 -- +--------+-----------+ 106 -- | deptno | avg_grade | 107 -- +--------+-----------+ 108 -- | 10 | 3.5000 | 109 -- | 20 | 3.0000 | 110 -- | 30 | 2.5000 | 111 -- +--------+-----------+
4.不准用聚合函数(max),取得最高薪水(给出两种方法)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- 方法一: 2 select sal from emp order by sal desc limit 1; 3 -- +---------+ 4 -- | sal | 5 -- +---------+ 6 -- | 5000.00 | 7 -- +---------+ 8 9 -- 方法二: 10 select 11 distinct 12 a.sal 13 from 14 emp a 15 join 16 emp b 17 on 18 a.sal > b.sal 19 20 21 select 22 e.ename, 23 e.sal 24 from 25 emp e 26 where 27 e.sal not in (select 28 distinct 29 a.sal 30 from 31 emp a 32 join 33 emp b 34 on 35 a.sal < b.sal);
5.取得平均薪水最高的部门的部门编号
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- 第一步:求出各部门的平均薪水,当做临时表 2 select 3 e.deptno,avg(e.sal) as avg_sal 4 from 5 emp e 6 group by 7 e.deptno 8 -- 第二步:求出最大值 9 select 10 max(avg_sal) as max_avg_sal 11 from 12 (select 13 e.deptno,avg(e.sal) as avg_sal 14 from 15 emp e 16 group by 17 e.deptno) a; 18 -- 第三步:没一组的平均薪水和最大值进行比较,如果相等则取出来 19 -- # 这样可以取出多个平均值一样的部门,避免遗漏的情况。 20 select 21 e.deptno,avg(e.sal) as avg_sal 22 from 23 emp e 24 25 group by 26 e.deptno 27 having 28 avg_sal=(select 29 max(avg_sal) as max_avg_sal 30 from 31 (select 32 e.deptno,avg(e.sal) as avg_sal 33 from 34 emp e 35 group by 36 e.deptno) a); 37 -- +--------+-------------+ 38 -- | deptno | avg_sal | 39 -- +--------+-------------+ 40 -- | 10 | 3150.000000 | 41 -- +--------+-------------+
6.取得平均薪水最高的部门的部门名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 思路和上面的一样,再和部门表做个表连接即可。 2 select 3 d.dname,avg(e.sal) as avg_sal 4 from 5 emp e 6 join 7 dept d 8 on 9 e.deptno=d.deptno 10 group by 11 e.deptno 12 having 13 avg_sal=(select 14 max(avg_sal) as max_avg_sal 15 from 16 (select 17 e.deptno,avg(e.sal) as avg_sal 18 from 19 emp e 20 group by 21 e.deptno) a); 22 -- +------------+-------------+ 23 -- | dname | avg_sal | 24 -- +------------+-------------+ 25 -- | ACCOUNTING | 3150.000000 | 26 -- +------------+-------------+
7.求平均薪水的等级最低的部门的部门名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 和上面的思路大致相同,找到最低的数值再和下面的数据进行比较 2 -- # 第一步:先求出每个部门的平均薪水以及等级 3 select 4 e.deptno, 5 avg(e.sal) as avg_sal 6 from 7 emp e 8 group by 9 e.deptno; 10 -- +--------+-------------+ 11 -- | deptno | avg_sal | 12 -- +--------+-------------+ 13 -- | 10 | 3150.000000 | 14 -- | 20 | 2220.833333 | 15 -- | 30 | 1566.666667 | 16 -- +--------+-------------+ 17 18 -- 将上表当做临时表 19 -- t(deptno,avg_sal) 20 21 22 -- # 第二步:查出每个平均薪水的等级,需要和salgrade做连接 23 select 24 t.deptno, 25 t.avg_sal, 26 s.grade 27 from 28 ( 29 select 30 e.deptno, 31 avg(e.sal) as avg_sal 32 from 33 emp e 34 group by 35 e.deptno 36 ) t 37 join 38 salgrade s 39 on 40 t.avg_sal between s.losal and s.hisal; 41 42 -- +--------+-------------+-------+ 43 -- | deptno | avg_sal | grade | 44 -- +--------+-------------+-------+ 45 -- | 30 | 1566.666667 | 3 | 46 -- | 20 | 2220.833333 | 4 | 47 -- | 10 | 3150.000000 | 5 | 48 -- +--------+-------------+-------+ 49 50 -- 第三步: 51 -- 找出上面的最小值 52 53 select 54 min(grade) as min_grade 55 from 56 ( 57 select 58 t.deptno, 59 t.avg_sal, 60 s.grade 61 from 62 ( 63 select 64 e.deptno, 65 avg(e.sal) as avg_sal 66 from 67 emp e 68 group by 69 e.deptno 70 ) t 71 join 72 salgrade s 73 on 74 t.avg_sal between s.losal and s.hisal 75 ) t; 76 77 -- 结果如下: 78 79 select 80 t.deptno, 81 d.dname, 82 t.avg_sal, 83 s.grade 84 from 85 ( 86 select 87 e.deptno, 88 avg(e.sal) as avg_sal 89 from 90 emp e 91 group by 92 e.deptno 93 ) t 94 join 95 salgrade s 96 on 97 t.avg_sal between s.losal and s.hisal 98 join 99 dept d 100 on 101 d.deptno=t.deptno 102 having s.grade=( 103 select 104 min(grade) as min_grade 105 from 106 ( 107 select 108 t.deptno, 109 t.avg_sal, 110 s.grade 111 from 112 ( 113 select 114 e.deptno, 115 avg(e.sal) as avg_sal 116 from 117 emp e 118 group by 119 e.deptno 120 ) t 121 join 122 salgrade s 123 on 124 t.avg_sal between s.losal and s.hisal 125 ) t 126 ); 127 -- +--------+-------+-------------+-------+ 128 -- | deptno | dname | avg_sal | grade | 129 -- +--------+-------+-------------+-------+ 130 -- | 30 | SALES | 1566.666667 | 3 | 131 -- +--------+-------+-------------+-------+ 132 -- # 总体思路就是,我先找出等级最小的,再将最小值去和每一个比较, 133 -- 值相同的就查出部门名称。
8.取得比普通员工(员工代码没有在mgr上出现的)的 最高薪水还要高的经理人姓名
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- 知识点: 2 -- not in 不会自动忽略空值 3 -- in会自动忽略空值 4 -- 第一步:先找出mgr 5 select distinct mgr from emp; 6 -- 第二步找出普通员工的ID: 7 select empno,sal from emp where empno not in (select distinct mgr from emp where mgr is not null); 8 -- +-------+---------+ 9 -- | empno | sal | 10 -- +-------+---------+ 11 -- | 7396 | 800.00 | 12 -- | 7499 | 1600.00 | 13 -- | 7521 | 1250.00 | 14 -- | 7654 | 1250.00 | 15 -- | 7844 | 1500.00 | 16 -- | 7876 | 1100.00 | 17 -- | 7900 | 950.00 | 18 -- | 7934 | 1300.00 | 19 -- +-------+---------+ 20 select 21 max(t.sal) as max_sal 22 from 23 (select empno,sal from emp where empno not in 24 (select distinct mgr from emp where mgr is not null)) t; 25 26 select 27 ename,sal 28 from 29 emp 30 where 31 emp.sal > ( 32 select 33 max(t.sal) as max_sal 34 from 35 (select empno,sal from emp where empno not in 36 (select distinct mgr from emp where mgr is not null)) t); 37 -- mysql> select 38 -- -> ename,sal 39 -- -> from 40 -- -> emp 41 -- -> where 42 -- -> emp.sal > ( 43 -- -> select 44 -- -> max(t.sal) as max_sal 45 -- -> from 46 -- -> (select empno,sal from emp where empno not in 47 -- -> (select distinct mgr from emp where mgr is not null)) t); 48 -- +-------+---------+ 49 -- | ename | sal | 50 -- +-------+---------+ 51 -- | JONES | 2975.00 | 52 -- | BLAKE | 2850.00 | 53 -- | CLARK | 2450.00 | 54 -- | SCOTT | 3000.00 | 55 -- | KING | 5000.00 | 56 -- | FORD | 3000.00 | 57 -- +-------+---------+
9.取得薪水最高的前五名员工
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select ename,sal from emp order by sal desc limit 0,5; 2 -- +-------+---------+ 3 -- | ename | sal | 4 -- +-------+---------+ 5 -- | KING | 5000.00 | 6 -- | SCOTT | 3000.00 | 7 -- | FORD | 3000.00 | 8 -- | JONES | 2975.00 | 9 -- | BLAKE | 2850.00 | 10 -- +-------+---------+
10.取得薪水最高的第六到第十名员工
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select ename,sal from emp order by sal desc limit 5,5; 2 -- +--------+---------+ 3 -- | ename | sal | 4 -- +--------+---------+ 5 -- | CLARK | 2450.00 | 6 -- | ALLEN | 1600.00 | 7 -- | TURNER | 1500.00 | 8 -- | MILLER | 1300.00 | 9 -- | MARTIN | 1250.00 | 10 -- +--------+---------+
11.取得最后入职的五名员工
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #按照入职时间来排序 入职晚 时间大 降序的话 排在前面 取前五个即可 2 select ename,sal from emp order by hiredate desc limit 0,5; 3 -- +--------+---------+------------+ 4 -- | ename | sal | hiredate | 5 -- +--------+---------+------------+ 6 -- | ADAMS | 1100.00 | 1987-05-23 | 7 -- | SCOTT | 3000.00 | 1987-04-19 | 8 -- | MILLER | 1300.00 | 1982-01-23 | 9 -- | FORD | 3000.00 | 1981-12-03 | 10 -- | JAMES | 950.00 | 1981-12-03 | 11 -- +--------+---------+------------+
12.取得每个薪水等级有多少个员工
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 先查出每个员工的薪水等级 2 select 3 e.ename, 4 e.sal, 5 s.grade 6 from 7 emp e 8 join 9 salgrade s 10 on 11 e.sal between s.losal and s.hisal; 12 -- +--------+---------+-------+ 13 -- | ename | sal | grade | 14 -- +--------+---------+-------+ 15 -- | SMITH | 800.00 | 1 | 16 -- | ALLEN | 1600.00 | 3 | 17 -- | WARD | 1250.00 | 2 | 18 -- | JONES | 2975.00 | 4 | 19 -- | MARTIN | 1250.00 | 2 | 20 -- | BLAKE | 2850.00 | 4 | 21 -- | CLARK | 2450.00 | 4 | 22 -- | SCOTT | 3000.00 | 4 | 23 -- | KING | 5000.00 | 5 | 24 -- | TURNER | 1500.00 | 3 | 25 -- | ADAMS | 1100.00 | 1 | 26 -- | JAMES | 950.00 | 1 | 27 -- | FORD | 3000.00 | 4 | 28 -- | MILLER | 1300.00 | 2 | 29 -- +--------+---------+-------+ 30 31 t(ename,sal,grade) 32 33 -- # 再用聚合函数根据薪水等级分类即可 34 select 35 t.grade , 36 count(t.grade) as grade_count 37 from 38 ( 39 select 40 e.ename, 41 e.sal, 42 s.grade 43 from 44 emp e 45 join 46 salgrade s 47 on 48 e.sal between s.losal and s.hisal 49 ) t 50 group by 51 grade; 52 -- +-------+-------------+ 53 -- | grade | grade_count | 54 -- +-------+-------------+ 55 -- | 1 | 3 | 56 -- | 2 | 3 | 57 -- | 3 | 2 | 58 -- | 4 | 5 | 59 -- | 5 | 1 | 60 -- +-------+-------------+
13.
14.列出所有员工及领导的名字
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 a.ename , 3 b.ename as boss 4 from 5 emp a 6 left join 7 emp b 8 on 9 a.mgr=b.empno; 10 -- +--------+-------+ 11 -- | ename | boss | 12 -- +--------+-------+ 13 -- | SMITH | FORD | 14 -- | ALLEN | BLAKE | 15 -- | WARD | BLAKE | 16 -- | JONES | KING | 17 -- | MARTIN | BLAKE | 18 -- | BLAKE | KING | 19 -- | CLARK | KING | 20 -- | SCOTT | JONES | 21 -- | KING | NULL | 22 -- | TURNER | BLAKE | 23 -- | ADAMS | SCOTT | 24 -- | JAMES | BLAKE | 25 -- | FORD | JONES | 26 -- | MILLER | CLARK | 27 -- +--------+-------+
15. 列出受雇日期早于其直接上级的
所有员工编号、姓名、 部门名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 a.empno, 3 a.ename , 4 d.dname 5 6 from 7 emp a 8 left join 9 emp b 10 on 11 a.mgr=b.empno 12 join 13 dept d 14 on 15 a.deptno=d.deptno 16 where 17 a.hiredate<b.hiredate; 18 19 -- +-------+-------+----------+ 20 -- | empno | ename | dname | 21 -- +-------+-------+----------+ 22 -- | 7396 | SMITH | RESEARCH | 23 -- | 7566 | JONES | RESEARCH | 24 -- | 7782 | CLARK | RESEARCH | 25 -- | 7499 | ALLEN | SALES | 26 -- | 7521 | WARD | SALES | 27 -- | 7698 | BLAKE | SALES | 28 -- +-------+-------+----------+
16.列出部门名称和这些部门的员工信息,同时列出那些 没有员工的部门
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 d.dname, 3 e.* 4 from 5 dept d 6 left join 7 emp e 8 on 9 d.deptno=e.deptno 10 -- +------------+-------+--------+-----------+------+------------+---------+---------+--------+ 11 -- | dname | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | 12 -- +------------+-------+--------+-----------+------+------------+---------+---------+--------+ 13 -- | ACCOUNTING | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | 14 -- | ACCOUNTING | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | 15 -- | RESEARCH | 7396 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 | 16 -- | RESEARCH | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | 17 -- | RESEARCH | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 20 | 18 -- | RESEARCH | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 | 19 -- | RESEARCH | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 | 20 -- | RESEARCH | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | 21 -- | SALES | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | 22 -- | SALES | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | 23 -- | SALES | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | 24 -- | SALES | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | 25 -- | SALES | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | 26 -- | SALES | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | 27 -- | OPERATIONS | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 28 -- +------------+-------+--------+-----------+------+------------+---------+---------+--------+
17.列出至少有5个员工的所有部门
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.deptno, 3 d.dname, 4 count(e.empno) as count_empno 5 from 6 emp e 7 join 8 dept d 9 on 10 e.deptno = d.deptno 11 group by 12 e.deptno 13 having 14 count_empno>5 15 16 -- +--------+----------+-------------+ 17 -- | deptno | dname | count_empno | 18 -- +--------+----------+-------------+ 19 -- | 20 | RESEARCH | 6 | 20 -- | 30 | SALES | 6 | 21 -- +--------+----------+-------------+
18. 列出薪水比“SMITH”多的所有员工信息
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- #第一步:先查出Smith的sal 2 select * from emp where sal>(select sal from emp where ename ='smith'); 3 select sal from emp where ename ='smith'; 4 -- (主要考察子查询) 5 6 -- +-------+--------+-----------+------+------------+---------+---------+--------+ 7 -- | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | 8 -- +-------+--------+-----------+------+------------+---------+---------+--------+ 9 -- | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | 10 -- | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | 11 -- | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | 12 -- | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | 13 -- | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | 14 -- | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 20 | 15 -- | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 | 16 -- | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | 17 -- | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | 18 -- | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 | 19 -- | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | 20 -- | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | 21 -- | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | 22 -- +-------+--------+-----------+------+------------+---------+---------+--------+
19.列出所有“CLERK”(办事员)的姓名及其部门名 称,部门人数
20.列出最低薪水大于1500的各种工作及从事此工作的全部雇员人数
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.job , 3 min(e.sal) as minsal, 4 count(e.empno) as countEmp 5 from 6 emp e 7 group by 8 job 9 having 10 minsal>1500; 11 -- +-----------+---------+----------+ 12 -- | job | minsal | countEmp | 13 -- +-----------+---------+----------+ 14 -- | ANALYST | 3000.00 | 2 | 15 -- | MANAGER | 2450.00 | 3 | 16 -- | PRESIDENT | 5000.00 | 1 | 17 -- +-----------+---------+----------+
21. 列出在部门“SALES”<销售部>工作的员工的姓名,假定不知道销售部门的部门编号
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.ename 3 from 4 emp e 5 join 6 dept d 7 on 8 e.deptno=d.deptno 9 where 10 d.dname='sales'; 11 -- +--------+ 12 -- | ename | 13 -- +--------+ 14 -- | ALLEN | 15 -- | WARD | 16 -- | MARTIN | 17 -- | BLAKE | 18 -- | TURNER | 19 -- | JAMES | 20 -- +--------+ 21 -- PS:还有一种办法就是先求出部门编号deptno
22.列出薪金高于公司平均薪金的所有员工,所在部门、 上级领导、雇员的工资等级
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 第一步 先求公司平均薪水 2 select 3 e.ename,e.sal,d.dname,b.ename as boss,s.grade 4 from 5 emp e 6 join 7 dept d 8 on 9 e.deptno = d.deptno 10 left join 11 emp b 12 on 13 e.mgr=b.empno 14 join 15 salgrade s 16 on 17 e.sal between s.losal and s.hisal 18 where 19 e.sal >(select avg(sal) as avg_sal from emp); 20 -- +-------+---------+------------+-------+-------+ 21 -- | ename | sal | dname | boss | grade | 22 -- +-------+---------+------------+-------+-------+ 23 -- | JONES | 2975.00 | RESEARCH | KING | 4 | 24 -- | BLAKE | 2850.00 | SALES | KING | 4 | 25 -- | CLARK | 2450.00 | RESEARCH | KING | 4 | 26 -- | SCOTT | 3000.00 | RESEARCH | JONES | 4 | 27 -- | KING | 5000.00 | ACCOUNTING | NULL | 5 | 28 -- | FORD | 3000.00 | RESEARCH | JONES | 4 | 29 -- +-------+---------+------------+-------+-------+
23. 列出与“SCOTT”从事相同工作的所有员工及部门名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 第一步:查出Scott的工作 2 select 3 job 4 from 5 emp 6 where 7 ename='scott'; 8 -- # 第二步查出部门 9 select 10 d.dname, 11 e.ename 12 from 13 emp e 14 join 15 dept d 16 on 17 e.deptno=d.deptno 18 where 19 job=( 20 select 21 job 22 from 23 emp 24 where 25 ename='scott' 26 ); 27 -- +----------+-------+ 28 -- | dname | ename | 29 -- +----------+-------+ 30 -- | RESEARCH | SCOTT | 31 -- | RESEARCH | FORD | 32 -- +----------+-------+
24.列出薪金等于部门30中员工的薪金的其它员工的姓名和薪金
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select sal from emp where deptno=30; 2 -- +---------+ 3 -- | sal | 4 -- +---------+ 5 -- | 1600.00 | 6 -- | 1250.00 | 7 -- | 1250.00 | 8 -- | 2850.00 | 9 -- | 1500.00 | 10 -- | 950.00 | 11 -- +---------+ 12 13 select ename ,sal from emp 14 where sal in (select distinct sal from emp where deptno=30) 15 and deptno<>30; 16 -- Empty set
25.列出薪金高于在部门30工作的所有员工的薪金的员工 姓名和薪金、部门名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 第一步:先找出部门30的最高薪水 2 select 3 max(sal) as maxsal 4 from 5 emp 6 where 7 deptno=30 8 9 select 10 e.ename, 11 e.sal, 12 d.dname 13 from 14 emp e 15 join 16 dept d 17 on 18 e.deptno=d.deptno 19 where 20 e.sal >( 21 select 22 max(sal) as maxsal 23 from 24 emp 25 where 26 deptno=30 27 ); 28 -- +-------+---------+------------+ 29 -- | ename | sal | dname | 30 -- +-------+---------+------------+ 31 -- | KING | 5000.00 | ACCOUNTING | 32 -- | JONES | 2975.00 | RESEARCH | 33 -- | SCOTT | 3000.00 | RESEARCH | 34 -- | FORD | 3000.00 | RESEARCH | 35 -- +-------+---------+------------+
26.列出在每个部门工作的员工数量、平均工资和平均服务期限
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- 求平均服务期限 现在的时间减去入职时间 再 求平均值即可。 2 select avg((to_days(now())-to_days(hiredate))/365) as avgserveryear from emp; 3 -- +---------------+ 4 -- | avgserveryear | 5 -- +---------------+ 6 -- | 37.08180039 | 7 -- +---------------+ 8 -- #再按照部门进行分类统计即可。
27. 列出所有员工的姓名、部门名称和工资
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.ename, 3 d.dname, 4 e.sal 5 from 6 emp e 7 join 8 dept d 9 on 10 e.deptno=d.deptno 11 -- +--------+------------+---------+ 12 -- | ename | dname | sal | 13 -- +--------+------------+---------+ 14 -- | KING | ACCOUNTING | 5000.00 | 15 -- | MILLER | ACCOUNTING | 1300.00 | 16 -- | SMITH | RESEARCH | 800.00 | 17 -- | JONES | RESEARCH | 2975.00 | 18 -- | CLARK | RESEARCH | 2450.00 | 19 -- | SCOTT | RESEARCH | 3000.00 | 20 -- | ADAMS | RESEARCH | 1100.00 | 21 -- | FORD | RESEARCH | 3000.00 | 22 -- | ALLEN | SALES | 1600.00 | 23 -- | WARD | SALES | 1250.00 | 24 -- | MARTIN | SALES | 1250.00 | 25 -- | BLAKE | SALES | 2850.00 | 26 -- | TURNER | SALES | 1500.00 | 27 -- | JAMES | SALES | 950.00 | 28 -- +--------+------------+---------+
28.列出所有部门的详细信息和人数
29.列出各种工作的最低工资及从事此工作的雇员姓名
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 job,min(sal) as minsal 3 from 4 emp e 5 group by 6 job 7 -- # 将上表当做临时表 8 9 select 10 e.sal,e.job,e.ename 11 from 12 emp e 13 join 14 ( 15 select 16 job,min(sal) as minsal 17 from 18 emp e 19 group by 20 job 21 ) t 22 on 23 e.job=t.job and e.sal = t.minsal; 24 -- +---------+-----------+--------+ 25 -- | sal | job | ename | 26 -- +---------+-----------+--------+ 27 -- | 800.00 | CLERK | SMITH | 28 -- | 1250.00 | SALESMAN | WARD | 29 -- | 1250.00 | SALESMAN | MARTIN | 30 -- | 2450.00 | MANAGER | CLARK | 31 -- | 3000.00 | ANALYST | SCOTT | 32 -- | 5000.00 | PRESIDENT | KING | 33 -- | 3000.00 | ANALYST | FORD | 34 -- +---------+-----------+--------+
30.列出各个部门MANAGER的最低薪金
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.deptno,min(e.sal) as minsal 3 from 4 emp e 5 where 6 e.job='manager' 7 group by 8 e.deptno; 9 -- +--------+---------+ 10 -- | deptno | minsal | 11 -- +--------+---------+ 12 -- | 20 | 2450.00 | 13 -- | 30 | 2850.00 | 14 -- +--------+---------+
31.列出所有员工的年工资,按年薪从低到高排序
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 ename ,sal*12 as yearsal 3 from 4 emp 5 order by 6 yearsal; 7 -- +--------+----------+ 8 -- | ename | yearsal | 9 -- +--------+----------+ 10 -- | SMITH | 9600.00 | 11 -- | JAMES | 11400.00 | 12 -- | ADAMS | 13200.00 | 13 -- | WARD | 15000.00 | 14 -- | MARTIN | 15000.00 | 15 -- | MILLER | 15600.00 | 16 -- | TURNER | 18000.00 | 17 -- | ALLEN | 19200.00 | 18 -- | CLARK | 29400.00 | 19 -- | BLAKE | 34200.00 | 20 -- | JONES | 35700.00 | 21 -- | FORD | 36000.00 | 22 -- | SCOTT | 36000.00 | 23 -- | KING | 60000.00 | 24 -- +--------+----------+
32.求出员工领导的薪水超过3000的员工名称和领导名称
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 e.ename,a.ename as boss ,a.sal 3 from 4 emp e 5 join 6 emp a 7 on 8 e.mgr = a.empno 9 where 10 a.sal>3000; 11 -- +-------+------+---------+ 12 -- | ename | boss | sal | 13 -- +-------+------+---------+ 14 -- | JONES | KING | 5000.00 | 15 -- | BLAKE | KING | 5000.00 | 16 -- | CLARK | KING | 5000.00 | 17 -- +-------+------+---------+
33.求部门名称中带“S”字符的部门员工的工资合计、 部门人数
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 select 2 d.dname, 3 sum(e.sal) as sumsal, 4 count(e.ename) as countEmp 5 from 6 emp e 7 join 8 dept d 9 on 10 e.deptno=d.deptno 11 where 12 d.dname like '%s%' 13 group by 14 d.dname; 15 -- +----------+----------+----------+ 16 -- | dname | sumsal | countEmp | 17 -- +----------+----------+----------+ 18 -- | RESEARCH | 13325.00 | 6 | 19 -- | SALES | 9400.00 | 6 | 20 -- +----------+----------+----------+
34. 给任职日期超过30年的员工加薪10%
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 -- # 先备份一下表 2 create table emp_bak as select * from emp; 3 update emp_bak set sal = sal *1.1 where (to_days(now())-to_days(hiredate))/365>30; 4 -- +-------+--------+-----------+------+------------+---------+---------+--------+ 5 -- | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | 6 -- +-------+--------+-----------+------+------------+---------+---------+--------+ 7 -- | 7396 | SMITH | CLERK | 7902 | 1980-12-17 | 880.00 | NULL | 20 | 8 -- | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1760.00 | 300.00 | 30 | 9 -- | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1375.00 | 500.00 | 30 | 10 -- | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 3272.50 | NULL | 20 | 11 -- | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1375.00 | 1400.00 | 30 | 12 -- | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 3135.00 | NULL | 30 | 13 -- | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2695.00 | NULL | 20 | 14 -- | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3300.00 | NULL | 20 | 15 -- | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5500.00 | NULL | 10 | 16 -- | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1650.00 | 0.00 | 30 | 17 -- | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1210.00 | NULL | 20 | 18 -- | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 1045.00 | NULL | 30 | 19 -- | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3300.00 | NULL | 20 | 20 -- | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1430.00 | NULL | 10 | 21 -- +-------+--------+-----------+------+------------+---------+---------+--------+