How do you find the Second highest Salary?

This is the most common question asked in Interviews.

EMPLOYEE table has fields EMP_ID and SALARY how do you find the second highest salary?

Answer:

We can write a sub-query to achieve the result

SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM EMPLOYEE)

The first sub-query in the WHERE clause will return the MAX SALARY in the table, the main query SELECT’s the MAX SALARY from the results which doesn’t have the highest SALARY.

64 Responses

  1. I really liked this post. :-)

  2. Excellent way to find that!

  3. SELECT TOP 1 stud_name,stud_marks
    FROM (SELECT TOP 2 stud_name, stud_marks FROM student ORDER BY stud_marks DESC) a
    ORDER BY stud_marks ASC

  4. excellent

  5. Please refer to following Sql Query it will work:
    select top 1 * from salary where salary in(select top 2 salary from salary order by desc)

  6. Kapil,
    The post submitted by you will not work dear,
    Reason: If your salar column having same value multiple time then this query will not show correct one,try this)

    the post submitted by employee is ok.

  7. Firstly I hate using the IN clause…. anyways the following query will work for…

    a. if there are multiple Highest salary….
    b. will list out names of all the second highest salary ….

    select name, salary from emp where salary =
    (select max(salary) from emp where salary

  8. Select Top 1 salary from emp where salary
    Not IN(select Top 1 salary from emp order by salary Desc)order by salary Desc

  9. select max(sal) from employee where sal

  10. select max(sal) from emp
    where sal

  11. select max(emp_sal) from salary where emp_sal not in ( select top(1) emp_sal
    from salary order by emp_sal desc)

  12. Hai alll

    i want query for 3rd highest salary in a table

    please guide me my mail id

    marni.ravikiran@hotmail.com

    thank u
    ravi

  13. Hi ALL,
    can any one tell me to find out the “nth highest salary”?
    thanx in advance.
    KMR

  14. Hi Ravi try 4 this u may get…

    SELECT [Employee_ID],
    [number],
    [name],
    [salary]
    FROM [Employee]
    WHERE [salary] = (
    SELECT MIN( [salary] )
    FROM (
    SELECT DISTINCT TOP 3 [salary]
    FROM [Employee]
    ORDER BY [salary] DESC
    ) [FourHighestDistinctSalaries]
    )

  15. How do you find the third highest Salary?

  16. i have a complete post related to this, in this article i have posted quries for both SQl Server 2000 and 2005 http://hemantg.blogspot.com/2007/06/retrieving-nth-salary-in-sql-server.html

  17. Thanks for this better solution

  18. If you want employee greatest salary ( 1st or 2nd or 3rd …. any thing ,in place of “N” in query, place the number)

    SELECT * FROM emptable e1 WHERE (N = (SELECT COUNT(DISTINCT (e2.empsalary))
    FROM emptable e2 WHERE e2.empsalary >= e1.empsalary))

  19. top 5th max salary of a employee in a employee table

    SELECT * FROM employee e1 WHERE (5 = (SELECT COUNT(DISTINCT (e2.emp_salary))
    FROM employee e2 WHERE e2.emp_salary >= e1.emp_salary))

  20. SELECT max(Salary)FROM Emp WHERE Salary <select MAX(Salary) from Emp

  21. select min(sal) from emp where (sal) IN (select top2 sal from emp);

  22. SELECT * FROM emptable e1 WHERE (N = (SELECT COUNT(DISTINCT (e2.empsalary))
    FROM emptable e2 WHERE e2.empsalary >= e1.empsalary))

    can any one explain this query whats the e1 & e2 tables

  23. How do you find the 4th highest Salary in sql server without using subquery? pls reply

  24. pls reply.
    How do you find the 4th highest Salary in sql server without using subquery?

  25. 3rd highest salary in a table

    Select Top 1 salary from empsal where salary
    Not IN(select Top 2 salary from empsal order by salary Desc)order by salary Desc

  26. TO FIND THE Nth HIGHEST SALARY

    SELECT MIN(SALARY) FROM (SELECT DISTINCT SALARY FROM EMPLOYEE ORDER BY SALARY DESC)WHERE ROWNUM=5;

  27. SELECT MIN(SALARY) FROM (SELECT DISTINCT SALARY FROM EMPLOYEE ORDER BY SALARY DESC)WHERE ROWNUM=N;

  28. I WANT TO CONNECT THE MULTIPLE USERS IN ORACLE1Og
    WHTS THE PROCESS

  29. How to find highest 2 salary from a table

  30. Thanks Dear..

    It is really helpful to complete my requirement…

  31. this is good site for us for search any question for sql as well as orecle thenks .

  32. SELECT * FROM temp_emp T1 WHERE
    4 = (SELECT COUNT(DISTINCT(T2.salary)) FROM temp_emp T2 WHERE T2.salary >= T1.salary);

  33. this site is good.
    how to join two different tables.

  34. Nice one. This post contains all the possible options to write the query.

  35. Too good. Thanks everyone for sharing the knowledge.

  36. ravi’s post rocks.

    Thanks Alot

  37. really the query helps…

  38. grt man………..

  39. how two find the second maximum number by using sqlserver without sub query

  40. select min(sal) from emp where sal in(select distinct top 2 sal from emp order by sal desc)

  41. 1. select max(esal) from emp where esal not in(select max(esal) from emp

    2.select esal from emp e1 where 2=(select count(distinct esal) from emp e2 where e2.esal>=e1.esal)

    3.select top2 * from emp where esal not in(select max(esal) from emp)

  42. select top 1 * from salary where salary in(select top 2 salary from salary order by desc)
    is not working

  43. select distinct sal from employee t
    where 3 = (select count(distinct sal)
    from employee
    where t.sal<=sal)

  44. I v have more than 10 equal and highest salary employee

  45. Select Top 1 salary from salary where salary
    Not IN(select Top 1 salary from salary order by salary Desc)order by salary Desc

    select max(salary) from salary where salary not in ( select Top 1 salary from salary order by salary desc)

  46. select trk_id from csl_trk a where 3=(select count (distinct(b.trk_id))from csl_trk b where a.trk_id<=b.trk_id);

  47. how to find second highest salary in postgreSQL.

  48. select distinct(Salary),* from Employee A where 3=(select count(distinct(Salary)) from Employee b where b.Salary <= a.Salary)

  49. nice

  50. only one

  51. select top 1 salary from employee where salary<(select max(salary) from
    employee) order by salary desc

    it will work for all condition for ms sql server 2005
    to find the second highest salary ……….

    SURENDRA GURJAR

  52. Here N is any number(you just put that number here and find that number of highest salary)
    This query takes data from two tables
    1.EMPLOYEE
    2.SALARY_DETAILS

    SELECT TOP 1 * FROM (SELECT TOP N * FROM (SELECT EMPLOYEE.FNAME,SALARY_DETAILS.SALARY FROM EMPLOYEE INNER JOIN SALARY_DETAILS ON EMPLOYEE.ID = SALARY_DETAILS.ID) AS D ORDER BY SALARY DESC) AS P ORDER BY SALARY

  53. I want to get the employee name as well for the second highest salary getter, if I do

    SELECT ENAME, MAX(SAL)
    FROM EMP
    WHERE SAL < (SELECT MAX(SAL) FROM EMP)

    or

    SELECT ENAME, MAX(SAL) AS SAL
    FROM EMP
    WHERE SAL NOT IN (SELECT MAX(SAL) FROM EMP)

    then an error message says
    “Column ‘emp.ENAME’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.”
    Plz provide a solution
    I donot want to use the TOP Function
    Regards
    Mahfooz

  54. How to find the second max salary for each deportments for the below examples.
    Table Name is EMP:

    Name Age Sal Dept
    ——————————————————
    AA 15 1000 10

    BB 20 3500 20

    CC 18 3000 10

    DD 21 2500 10

    EE 19 3000 20

    Expected Output is,

    Sal Dept
    —————————-
    3000 10

    3500 20

    Please share your valuable information.

  55. That’s a bit tricky ……. I just lost 10 marks due to this ……….. ::(

  56. How to find second highest value of a column in a table? – SQL …

    Declare @temp int

    set @temp = 2

    SELECT MAX(SALARY) from EMPLOYEE_PAY_TBL WHERE SALARY NOT IN ( SELECT TOP (@temp – 1) SALARY FROM EMPLOYEE_PAY_TBL ORDER BY SALARY DESC)

  57. Gud Site

  58. Please mention Oracle Version for usage of TOP keyword.

  59. Select emp_sal from salary where emp_sal not IN (Select max(emp_sal) from salary) ORDER BY emp_sal DESC LIMIT 0 , 1

  60. Excellent code

  61. how 2 find the top 5 salaries of employees in the employee table.

  62. how 2 enter one system from another system when they r connected in network,and how 2 trace the second system password.

  63. select * from employee orderby salary DESC
    Limit 1,1

  64. My answer for same questions

    SELECT SALARY FROM EMPLOYEE WHERE Order by SALARY Desc limit 1,1.

Leave a Reply