Nth Highest Salary in SQL – Explained with Examples

 SQL is one of the most powerful tools for managing and analyzing data. Whether you're preparing for a job interview or building real-world applications, understanding how to find the Nth highest salary from a table is a must-have skill.

In this blog, we’ll walk through the concept of the Nth highest salary, different methods to find it, and which technique is best depending on your SQL environment.


Understanding the Problem

Imagine you have an employees table with various salaries. You need to write a query to find:

  • The 1st highest salary

  • The 2nd highest salary

  • The 3rd highest salary

  • And so on...

This is what’s known as finding the “Nth” highest salary.

Here’s an example table:

+----+--------+--------+ | id | name | salary | +----+--------+--------+ | 1 | Rahul | 75000 | | 2 | Anita | 68000 | | 3 | Mohan | 75000 | | 4 | Seema | 85000 | | 5 | Deepak | 60000 | +----+--------+--------+

We’ll now explore three commonly used solutions.


Solution 1: Using ORDER BY with LIMIT and OFFSET (MySQL)

This is one of the easiest ways to get the Nth highest salary in MySQL:

SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 2;

Explanation:

  • ORDER BY salary DESC: sorts salaries from high to low.

  • OFFSET 2: skips the first two rows.

  • LIMIT 1: returns the next one — i.e., the 3rd highest salary.

Change the OFFSET value to get a different N.


Solution 2: Using Subqueries

If your SQL version doesn’t support advanced functions, you can still get the result using nested subqueries.

Here’s how to find the 3rd highest salary

SELECT MAX(salary) FROM employees WHERE salary < ( SELECT MAX(salary) FROM employees WHERE salary < ( SELECT MAX(salary) FROM employees ) );

For the 4th highest, just nest one more level. Although this works well, it can become difficult to maintain for larger N values.


Solution 3: Using DENSE_RANK() for Accuracy and Simplicity

If your database supports window functions, this is the best method:


SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees ) AS ranked WHERE rnk = 3;

Why use DENSE_RANK?

Because it handles duplicate salary values correctly. If two employees share the same salary, they’ll be ranked the same, which is usually what you want when calculating the Nth highest.


When to Use Which Meth

Learn SQL the Right Way at Technogeeks

At Technogeeks, we teach SQL from the ground up — including advanced queries like this — with real-life examples, hands-on labs, and placement support.

Our course covers:

  • MySQL, PostgreSQL, Oracle basics to advanced

  • Real-time business use cases and reports

  • Interview questions from MNCs and startups

  • Practice projects with ranking, joins, and analytics

Whether you're from a technical or non-technical background, our trainers will help you master SQL at your pace.


Final Words

The Nth highest salary question might seem tricky, but with the right learning approach, it becomes second nature. SQL isn’t just about writing queries — it's about solving problems with data. And that's what you’ll learn at Technogeeks.

Comments

Popular posts from this blog

Python features

How to Find GCD of Two Numbers in Java

Master AWS with the Best Training in Pune – Join Technogeeks Today!