mysql查询工资大于某个员工_MySQL新手:查询查找每个员工的姓名,其薪水超过其所在部门所有员工的平均工资 (MySQL newbie: Query to find the name of eac...

本文介绍了一种使用SQL子查询的方法,通过连接多个表并分组部门来找出每个部门中薪资高于该部门平均薪资水平的员工。展示了具体的SQL查询语句,并解释了各个部分的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Your approach using a subquery for the average is sound, but you need to group the subquery by department. Then you can join to the subquery by:

Department ID's are equal (equijoin), and

Employee salary is greater than than the average departmental salary (non-equijoin)

Here's the query...

SELECT emp.ename, dept.dname, emp.salary, DeptAvg.AvgSal

FROM emp

INNER JOIN works ON emp.eid = works.eid

INNER JOIN dept ON works.did = dept.did

INNER JOIN (

SELECT works.did, AVG(emp.salary) AS AvgSal

FROM emp

INNER JOIN works ON emp.eid = works.eid

GROUP BY works.did) DeptAvg

ON DeptAvg.did = works.did AND emp.salary > DeptAvg.AvgSal

This query shows employee name, department name, employee salary and average departmental salary. I did that so you can see the numbers and test it. You can remove any of the columns and the query should still work.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值