sql - Maximum salary of employee in specific department -


empid   empname empsalary   empdept 1       steve    5000       hr 2       robert   5000       management 3       brad     3000       hr 4       sam      4000       hr 5       dave     2000       management 6       stuvart  4500       management 

how employee details employee table salary max , belong hr department... query

select empid,empname,empsalary,empdept  employee  empsalary in (select max(empsalary) employee)  , empdept='hr' 

i tried above query, giving me accurate result, due performance issue can not use inner queries.

you can use order by clause rownum oracle version < 12c:

select empid, empname, empsalary, empdept  employee  rownum = 1 , empdept = 'hr' order empsalary desc 

otherwise can use following:

select empid, empname, empsalary, empdept  employee  empdept = 'hr' order empsalary desc fetch first row ties 

p.s.: with ties option brings opportunity multiple results in case there multiple employees same max salary (so resulting set may differ 1 using rownum, there 1 row in resulting set).


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -