Manoj Kumar Duraisamy
Get the second highest value from the table using SQL

We have table called Employee and It has some random records with EmployeeID,EmployeeName and Employee Salary. In that how you will get second highest salary using sql query

By Manoj Kumar Duraisamy in SQL on Jan 07 2023
  • Munib Butt
    Jan, 2023 12

    SELECT MIN(EmployeeSalary)
    FROM (SELECT DISTINCT TOP (2) EmployeeSalary
    FROM Employee
    ORDER BY EmployeeSalary DESC)T
    HAVING MIN(EmployeeSalary) <> MAX(EmployeeSalary);

    • 2
  • Digambar Bhandare
    Jan, 2023 28

    Select top 1 *from(select top 2 salary from TblEmployee order by salary desc ) as total order by salary asc

    • 1
  • Umang Pawar
    May, 2023 7

    This soln is only for 2nd highest.

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

    for nth highest replace n with any value:

    select top 1 salary from (
    select top N salary from employee order by salary desc) result
    order by salary

    another one:

    with cte as(
    select salary,dense_rank() over (order by salary desc) rank from employee )
    select salary from cte where rank = n;

    Follow for more solutions.

    • 0
  • Tuhin Paul
    Apr, 2023 15

    1. SELECT TOP 1 EmployeeSalary
    2. FROM (
    3. SELECT DISTINCT TOP 2 EmployeeSalary
    4. FROM Employee
    5. ORDER BY EmployeeSalary DESC
    6. ) AS Salary
    7. ORDER BY EmployeeSalary ASC;
    1. We start by selecting the top 2 unique salary values from the Employee table in descending order, using the DISTINCT and TOP clauses, respectively.
    2. We wrap this query in a subquery and alias it as Salary.
    3. We select the top 1 salary value from the subquery in ascending order using the TOP and ORDER BY clauses.

    • 0
  • Tuhin Paul
    Apr, 2023 15

    To get the second highest salary from the Employee table, you can use the SELECT TOP clause along with the ORDER BY clause in SQL.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS