To find the second highest salary, We can use various methods. Here are two common method:
Method 1: Using DISTINCT
with ORDER BY
This method is straightforward and works well when there are no duplicates or when you want the second distinct highest salary.It selects the top salary that is less than the maximum salary in the table, effectively giving you the second highest salary.
Method 2: Using the ROW_NUMBER()
Function:
This method is effective even when there are duplicate salaries and you want to rank them.The ROW_NUMBER()
function assigns a unique rank to each salary based on descending order. You then filter for the second rank.