Skip to content
Advertisement

get 2nd highest sum in mysql

i have table emp :

i want to get the id having 2nd highest sum

let me know how can this be done?

my query is giving error at line: WHERE t.rownum=2 However if i remove this line it works fine. Let me know how can i put this condition

Advertisement

Answer

DENSE_RANK() needed (not RANK()) because if, for example, there is 2 rows with highest sum then they’ll have RANK and DENSE_RANK equal to 1, but for the next sum value DENSE_RANK will be 2 whereas single RANK will be 3.

fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement