Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

mysql - 查找具有最高平均工资的部门的名称(Find the name of the department having the maximum average salary)

I tried with the following query to get department name but not achieving completely.

(我尝试使用以下查询获取部门名称,但未完全实现。)

select Dep_name as dept_name
from salary
group by Dep_name
order by avg_salary desc
limit 1;

the table looks like

(桌子看起来像)

emp_id  Dep_name     salary
34      Marketing    35000
35      Sales        48000
36      Admin        23000
37      Sales        67000
38      Sales        97000
39      Marketing    98000
40      Admin        57000
41      Admin        71000
42      Marketing    80000  

getting output as 'Sales' ie highest Avg_salary among the departments

(获得“销售”输出,即部门中最高的平均工资)

I guess the output should be 'Marketing'

(我猜输出应该是“营销”)

  ask by Yashaswini Ravi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this will work:

(这将工作:)

select avg(salary),Dep_name from Table1
group by Dep_name
order by avg(salary) desc
limit 1;

http://sqlfiddle.com/#!9/0efe8c/11

(http://sqlfiddle.com/#!9/0efe8c/11)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...