How to Find the Nth Highest Salary in SQL – A Guide for Beginners
In SQL interviews, one of the most frequently asked and important questions is:
"How do you find the Nth highest salary from a table?"
This question tests your understanding of sorting, filtering, and subqueries. It may seem complex at first, but once you learn a few different techniques, it becomes an easy and essential part of your SQL skill set.
In this blog, we’ll explore what the question means, and how to solve it using multiple approaches, including simple and advanced SQL queries.
What is the Nth Highest Salary?
Let’s say you have an employees table like this:
You need to find the salary that stands at the 2nd, 3rd, or 4th position when all salaries are sorted in descending order. For example:
-
1st highest = 80000
-
2nd highest = 70000
-
3rd highest = 60000
Let’s see how to achieve this.
Method 1: Using LIMIT and OFFSET (MySQL)
If you are using MySQL, this is one of the simplest ways to find the Nth highest salary:
Explanation:
-
ORDER BY salary DESCsorts salaries from highest to lowest. -
OFFSET 2skips the top 2 highest salaries. -
LIMIT 1fetches the next one, i.e., the 3rd highest salary.
To get the 2nd highest, use OFFSET 1. For 4th highest, use OFFSET 3, and so on.
Method 2: Using Subquery
This method is more database-independent and works in almost all SQL environments:
This example will return the 3rd highest salary. You can nest more subqueries depending on the value of N.
Method 3: Using DENSE_RANK() with a CTE or Subquery
If your SQL database supports window functions (like PostgreSQL, SQL Server, or Oracle), you can use DENSE_RANK():
This approach is accurate even if salaries are repeated. Unlike ROW_NUMBER(), DENSE_RANK() assigns the same rank to duplicate salary values.
Best Practices
-
If your database supports it, prefer using
DENSE_RANK()for readability and accuracy. -
Use subqueries when working with databases that do not support window functions.
-
Always use
DISTINCTin queries when duplicate values can affect the result.
Why Learn This at Technogeeks?
At Technogeeks, we believe in teaching SQL with practical, real-world examples. This Nth highest salary problem is not only important for interviews but also reflects real tasks you'll encounter in data analysis and reporting roles.
Our SQL training includes:
-
Real-time use cases and database projects
-
Interview preparation and mock questions
-
Coverage of MySQL, PostgreSQL, and Oracle queries
-
Practice assignments on sorting, ranking, and advanced filtering
Whether you are preparing for a Data Analyst, Backend Developer, or Business Intelligence role — mastering SQL is essential, and Technogeeks is here to help you learn it right.
Final Thoughts
The Nth highest salary problem is more than just a question — it’s a stepping stone to understanding how SQL handles data ranking and filtering. Practice it well, and you’ll find yourself more confident in interviews and real-world database scenarios.
Comments
Post a Comment