mysql连接查询练习题(⼀)mysql连接查询练习题(⼀)
⼀.题⽬所需的三张表
dept(部门表)
emp(员⼯表)
salgrade(薪⽔级别表)
⼆.习题
1.取得每个部门最⾼薪⽔的⼈员名称
取得每个部门的最⾼薪⽔
select deptno,max(sal)as maxsal from emp group by deptno;//每部门最⾼薪⽔
连接这张表进⾏查询
select
from
(select deptno,max(sal)as maxsal from emp group by deptno)t join
emp e
on
t.deptno=e.deptno and t.maxsal=e.sal;
2.哪些⼈的薪⽔在部门的平均薪⽔之上
取得每个部门的平均薪⽔
application中文翻译进⾏连接查询
select
from
clickhouse数组转字符串emp e
join
(select deptno,avg(sal)as avgsal from emp group by deptno)t on
t.deptno=e.deptno and e.sal>t.avgsal;
注意on后⾯的条件⼀定要有t.deptno=e.deptno,否则:
每个员⼯的薪⽔会和所有部门的平均薪⽔⽐较,满⾜就显⽰。正解如下:
3.取得部门中平均薪⽔的等级
取得每个部门的平均薪⽔
进⾏连接查询
mysql面试题库select
t.*,s.grade
from
(select deptno,avg(sal)as avgsal from emp group by deptno)t
join
salgrade s
on
t.avgsal between s.local and s.hisal;
4.取得平均薪⽔最⾼的部门的部门编号(平均薪⽔可能相同)
取得每个部门的平均薪⽔
对分组的部门薪⽔通过having再次进⾏筛选
select
deptno,avg(sal)as avgsal
from
emp group by deptno
having
avgsal=(select max(t.avgsal)from
(select avg(sal) avgsal from emp group by deptno) t)
5.取得平均薪⽔最⾼的部门的部门名称
取得每个部门的名称与平均部门薪⽔
select
d.dname,avg(
e.sal) avgsal
from
emp e
join
dept d
on
e.deptno=d.deptno
group by
d.dname;
javascript中case什么意思对结果⽤having进⾏过滤(条件是等于最⾼的部门薪⽔)
select
d.dname,avg(
e.sal) avgsal
from
emp e
join
dept d
on
e.deptno=d.deptno
group by
d.dname
having
avgsal=(select max(t.avgsal)from
(select avg(sal) avgsal from emp group by deptno) t)
6.求平均薪⽔的等级最⾼的部门的部门名称(平均薪⽔不是最⾼,但等级最⾼)
取得每个部门平均薪⽔的等级
select
t.*,s.grade
from
(select deptno,avg(sal) avgsal from emp group by deptno)t
join
salgrade ssmart油耗设置
on
t.avgsal between s.losal and s.hisal;
取得每个部门平均薪⽔的等级和部门名称
select
t.*,s.grade
from
(select d.dname,avg(e.sal) avgsal from emp e join dept d on d.deptno=e.deptno group by d.dname)t
join
salgrade s
on
t.avgsal between s.losal and s.hisal;
对结果使⽤where条件对结果进⾏筛选(having是⽤在group by 之后)
select
t.*,s.grade
from
(select d.dname,avg(e.sal) avgsal from emp e join dept d on d.deptno=e.deptno group by d.dname)t
join
salgrade s
on
图片和文本框组合不了t.avgsal between s.losal and s.hisal
where
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论