Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Write a query to find second highest salary of an employee?

user-image
Question added by Muhammad Shahid Farooq , Software Engineer, System Integrator , ForeVision Business Solutions
Date Posted: 2017/03/16
Bindu Thomas
by Bindu Thomas , Senior Programmer Analyst , Future Trends Establishment

select max(salary) from employees where salary < (select max(salary) from employees)

Srihari Macha
by Srihari Macha , Porject Lead , Syntel Ltd

select * from emp where salary = ( select salary from (select ROW_NUMBER() over (order by salary) as 'rownum', * from emp) t -- Order employees according to salary where rownum = 2 -- Get the second highest salary )

Raghul V
by Raghul V , Oracle Developer , Al Mulla Group EST

select * from employee

where salary = (select  max(salary) from employee e 

where salary<(select max(salary) from employee))

 

Qaisar Mushtaq
by Qaisar Mushtaq , Senior Consultant , Systems Limitied

select *

  from (select employee_name,

               salary,

               DENSE_RANK() OVER(ORDER BY salary) sal_rank

          from employees)

 where sal_rank = 2;

 

Select Max(sal) from Employee where sal Not In(Select Max(sal) From Emp)

MEERAN MOHIDEEN
by MEERAN MOHIDEEN , Software Engineer , C-Square Info Solutions Pvt Ltd

WITH query_a AS 

( SELECT DENSE_RANK() OVER(ORDER BY sal DESC) sal_rank,

             employee, 

             sal

  FROM employee )

SELECT employee,

            sal

  FROM query_a

WHERE sal_rank = 2;

 

PRASAD BAGAREGARI
by PRASAD BAGAREGARI , Sr.Associate / Senior Software Engineer , BNP PARIBAS INDIA SOLUTIONS LTD

select distinct sal from employee a where &N = ( select count(distinct(b.sal) from employee b where a.sal <= b.sal) 

Replace N by number which is requied like if its second max then N =2 and so on....

helmy yoga pranata
by helmy yoga pranata , Database administrator , Ministry of Home Affairs

other modification :

 select * from (select  rownum rank, a.* from

    (select salary from employees 

    orderby salary desc)a) where rank=2;

Hemant Kumar
by Hemant Kumar , Infor WMS Admin, Functional Consultant and Support , Al Khyyat Investment

select * from (select salary,row_number() over(order by salary desc) rn from employee) where rn =2;

Aburar Yaseen
by Aburar Yaseen , Sr.Developer , Cognizant Technology Solutions

select emp_id, salary

from(

     select emp_id,salary,row_number() over (order by salary desc) as salary_rank

     from emp

) sub

where salary_rank = 2 ;

Maroun Merhej
by Maroun Merhej , Development Supervisor , BML Istisharat

having the empno as the key

select tb.* from (select t.*,row_number() over (partition by t.empno order by t.salary desc) salno  from employee t) tb where tb.salno=2

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.