Interview

Interview Question & Answer SQL interview.

Written by shohal

Interview Question & Answer SQL interview

How to find nth highest salary in sql

1. How to find nth highest salary in SQL Server using a Sub-Query
2. How to find nth highest salary in SQL Server using a CTE
3. How to find the 2nd, 3rd or 15th highest salary

Let’s use the following Employees table for this demo.

To find the highest salary it is straight forward. We can simply use the Max() function as shown below.

To get the second highest salary use a sub query along with Max() function as shown below.

To find nth highest salary using Sub-Query

To find nth highest salary using CTE

To find 2nd highest salary we can use any of the above queries. Simple replace N with 2. 

Similarly, to find 3rd highest salary, simple replace N with 3. 

Please Note: On many of the websites, you may have seen that, the following query can be used to get the nth highest salary. The below query will only work if there are no duplicates.

🚀

If you’d like me to proceed with any of these, please just let me know from the site techtweet.xyz! Also if you need to learn something new than subscribe YouTube : ASP.NET With SQL SERVER

About the author

shohal

Leave a Comment