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.

By Naren

Hi, I'm Naren, a SQL Server DBA and Developer with a bachelor's degree in Computer Science Engineering. I bring a wealth of professional experience to the table, ensuring that the content of this article is grounded in real-world knowledge and expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *