sql - Find out max value for each group along with name -
i have table employee contents below
------------------------------------------------------------------- name department salary ------------------------------------------------------------------- hemant pts 100 gunjan pts 101 amol adm 50 parthiv adm 60 -------------------------------------------------------------------
i want write single query display name of highest salary consumer group pts. in other words output of query should gunjan , parthiv different department , earning highest salary in department.
------------------------------------------------------------------- name department salary ------------------------------------------------------------------- gunjan pts 101 parthiv adm 60 -------------------------------------------------------------------
below query working properly
select e.department,max(e.salary) employee e group department;
but how can select name along department , max(salary)?
since have not mentioned rdbms, query below work on rdbms (but not guess)
select a.* employee inner join ( select department, max(salary) max_val employee group department ) b on a.department = b.department , a.salary = b.max_val
output
╔═════════╦════════════╦════════╗ ║ name ║ department ║ salary ║ ╠═════════╬════════════╬════════╣ ║ gunjan ║ pts ║ 101 ║ ║ parthiv ║ adm ║ 60 ║ ╚═════════╩════════════╩════════╝
Comments
Post a Comment