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
SELECT MIN(EmployeeSalary)FROM (SELECT DISTINCT TOP (2) EmployeeSalary FROM Employee ORDER BY EmployeeSalary DESC)THAVING MIN(EmployeeSalary) <> MAX(EmployeeSalary);
Select top 1 *from(select top 2 salary from TblEmployee order by salary desc ) as total order by salary asc
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) resultorder 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.
SELECT TOP 1 EmployeeSalaryFROM ( SELECT DISTINCT TOP 2 EmployeeSalary FROM Employee ORDER BY EmployeeSalary DESC) AS SalaryORDER BY EmployeeSalary ASC;
SELECT TOP 1 EmployeeSalary
FROM (
SELECT DISTINCT TOP 2 EmployeeSalary
FROM Employee
ORDER BY EmployeeSalary DESC
) AS Salary
ORDER BY EmployeeSalary ASC;
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.