1.求每个部门最高薪水的人员名称
解决思路:1 先求每个部门最高的薪水
SELECT e.deptno,MAX(e.sal) as maxsalfrom emp eGROUP BY e.deptno;
2. 将第一步得出来的结果,当成临时表
SELECT e.deptno,ename,t.maxsal,e.salfrom (SELECT e.deptno,max(e.sal) as maxsal from emp e GROUP BY e.deptno) tJOIN emp eon t.deptno = e.deptnoWHERE t.maxsal = e.salORDER BY --做一个排序 e.deptno;
- 哪些人的薪水在部门平均薪水之上
- 求出每个部门的平均薪水
SELECT e.deptno,avg(e.sal) as avgsalfrom emp eGROUP BY e.deptno;
2. 查看哪些人的薪水大于平均水平
SELECT e.deptno,e.ename,e.salfrom (SELECT e.deptno,avg(e.sal) as avgsalfrom emp eGROUP BY e.deptno) tJOIN emp eON e.deptno = t.deptnoWHERE t.avgsal < e.sal
3.取得部门中(所有人的)平均薪水等级
- 取得部门中所有人的平均薪水的等级
SELECT e.deptno,avg(e.sal) as avgsalfrom emp eGROUP BY e.deptno;
- 取得部门中所有人的平均的薪水等级
SELECT t.deptno,t.avgsal,s.gradefrom ( SELECT e.deptno,avg(e.sal) as avgsal from emp e GROUP BY e.deptno) tJOIN salgrade son t.avgsal BETWEEN s.losal and hisal
- 取得部门中所有人的平均的薪水等级
- 求出每个人的薪水等级
SELECT e.deptno,e.ename,s.gradeFROM emp ejoin salgrade son e.sal BETWEEN s.losal and s.hisal;
2. 在临时表求平均的
SELECT t.deptno,t.ename,AVG(t.grade) as avggradeFROM (SELECT e.deptno,e.ename,s.gradeFROM emp ejoin salgrade son e.sal BETWEEN s.losal and s.hisal) tGROUP BY t.deptno
- 不用组函数(MAX),求取最高薪水(俩种方案)
- 用倒叙,找第一行
SELECT e.sal from emp e ORDER BY e.sal desc LIMIT 1
- 建俩张表,交叉进行比较去掉最大的,在找出不在俩表生成的表中数,就是最大的数
SELECT a.sal FROM emp a WHERE a.sal not in (SELECT a.sal from emp a join emp bon a.sal < b.sal);
- 求取平均薪水最高的部门的部门编号
- 求部门平均水平
SELECT e.deptno,avg(e.sal) as avgsalFROM emp eGROUP BY e.deptno
2. 求出平均水平最大值,再进行筛选
SELECT t.deptno FROM ( SELECT e.deptno,avg(e.sal) as avgsal FROM emp e GROUP BY e.deptno)tHAVING MAX(avgsal)
7 . 求取平均薪水最高部门的部门名称
SELECT d.dnameFROM ( SELECT e.deptno,avg(e.sal) as avgsal FROM emp e GROUP BY e.deptno)tJOIN dept dON t.deptno = d.deptnoHAVING MAX(avgsal)
8.求平均薪水的等级最低的部门名称
- 部门的平均薪水
SELECT e.deptno,avg(e.sal) AS avgsalFROM emp egroup by e.deptno
- 在一的基础上求最低的部门薪资等级
SELECT MIN(s.grade)from (SELECT e.deptno,avg(e.sal) AS avgsal FROM emp e group by e.deptno) tjoin salgrade s on t.avgsal between s.losal and s.hisal
3.连表就行求部门名称
SELECT t.deptno,t.dname,s.gradeFROM (SELECT e.deptno,d.dname,avg(e.sal) as avgsal FROM emp e JOIN dept d on e.deptno = d.deptno GROUP BY e.deptno,d.dname)tJOIN salgrade son t.avgsal BETWEEN s.losal and s.hisalWHERE s.grade = (SELECT MIN(s.grade)from (SELECT e.deptno,avg(e.sal) AS avgsal FROM emp e group by e.deptno) tjoin salgrade s on t.avgsal between s.losal and s.hisal )
- 求比普通员工的最高薪水高的经理名字
- 找出mgr的人有哪些
SELECT DISTINCT --去重 mgr FROM emp
- 找出最高薪水员工
SELECT max(sal) as maxsalFROM emp WHERE empno not in (SELECT DISTINCT mgr FROM emp WHERE mgr is not null)
注 in 会自动忽略空值,not in不会自动忽略
SELECT ename from emp WHERE sal > (SELECT max(sal) as maxsalFROM emp WHERE empno not in (SELECT DISTINCT mgr FROM emp WHERE mgr is not null) )
10 取薪水最高的前五名员工
SELECT * from emp ORDER BY sal desc LIMIT 0,5;
11 取得薪水最高的第六到第十名员工
SELECT * from emp ORDER BY sal desc limit 5,5
12 取得最后入职的5名员工
SELECT * from emp order by hiredate desc limit 0,5
13求每个等级有多少员工
1.查询每个员工的薪水等级
SELECT e.ename,s.gradeFROM emp eJOIN salgrade son e.sal BETWEEN s.losal and s.hisalORDER BY s.grade
- 将每个等级的员工计数
SELECT t.grade ,COUNT(t.ename) as countEmpFROM (SELECT e.ename,s.gradeFROM emp eJOIN salgrade son e.sal BETWEEN s.losal and s.hisalORDER BY s.grade) tGROUP BY t.grade
原文转载:http://www.shaoqun.com/a/709737.html
麦言:https://www.ikjzd.com/w/1456
易速:https://www.ikjzd.com/w/2389
1.求每个部门最高薪水的人员名称解决思路:1先求每个部门最高的薪水SELECTe.deptno,MAX(e.sal)asmaxsalfromempeGROUPBYe.deptno;2.将第一步得出来的结果,当成临时表SELECTe.deptno,ename,t.maxsal,e.salfrom(SELECTe.deptno,max(e.sal)asmaxsalfromempeGROUPBYe.de
笨鸟海淘:https://www.ikjzd.com/w/1550
亚马逊t恤:https://www.ikjzd.com/w/1932
acca是什么:https://www.ikjzd.com/w/1370
口述:我和男友爱爱时被他后妈扇耳光婆婆后妈爱爱:http://lady.shaoqun.com/m/a/29672.html
商标查询:https://www.ikjzd.com/w/710
恩人是好色男 要我用处女身报答:http://www.30bags.com/a/253521.html
No comments:
Post a Comment