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
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
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.
SELECT TOP 1 EmployeeSalaryFROM (SELECT DISTINCT TOP 2 EmployeeSalaryFROM EmployeeORDER BY EmployeeSalary DESC) AS SalaryORDER 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.
SELECT MIN(EmployeeSalary)
FROM (SELECT DISTINCT TOP (2) EmployeeSalary
FROM Employee
ORDER BY EmployeeSalary DESC)T
HAVING MIN(EmployeeSalary) <> MAX(EmployeeSalary);