Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 140

Find Nth Highest Salary in MySQL

In this lesson: Find 2nd, 3rd or Nth highest salary in MySQL using LIMIT OFFSET, subquery and DENSE_RANK — interview favourite. In Hindi & English.

A classic interview question

"Find the Nth highest salary" is one of the most asked SQL interview questions. It tests your understanding of sorting, LIMIT, OFFSET and duplicates. There are a few clean ways to solve it — LIMIT/OFFSET, and DENSE_RANK on modern MySQL.

💡 Sort salaries descending, then skip the first N-1 distinct values to reach the Nth highest.

The second highest

SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
+--------+ | salary | +--------+ | 70000 | +--------+

Sort salaries from highest to lowest, then OFFSET 1 skips the top one, and LIMIT 1 returns the next — the second highest.

The Nth highest with OFFSET

-- Nth highest: OFFSET is N-1
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;   -- 3rd highest

For the Nth highest, use OFFSET N-1. To get the 3rd highest, offset 2. This general pattern finds any rank you want.

Handling duplicate salaries

DISTINCT is important. If three employees all earn the top salary, they would fill three rows, and the offset would skip within the same salary. DISTINCT collapses equal salaries into one, so "second highest" means the second distinct salary, which is what people usually mean.

The DENSE_RANK way

SELECT salary FROM (
    SELECT salary,
        DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
    FROM employees
) t
WHERE rnk = 2;   -- 2nd highest

In MySQL 8.0+, DENSE_RANK() gives each salary a rank, with equal salaries sharing a rank and no gaps. Filtering rnk = N reads very clearly and handles ties naturally.

The subquery method

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

For the second highest specifically, this classic subquery finds the highest salary below the maximum. It is neat for N=2, though LIMIT/OFFSET or DENSE_RANK scale better for any N.

Common mistakes

  • Forgetting DISTINCT. Duplicate salaries then break the offset.
  • Off-by-one in OFFSET. The Nth highest uses OFFSET N-1.
  • Using DENSE_RANK on old MySQL. Window functions need MySQL 8.0+.
  • Forgetting DESC. Without it you get the Nth lowest, not highest.

Summary

  • Nth highest: ORDER BY salary DESC LIMIT 1 OFFSET N-1 with DISTINCT.
  • Second highest uses OFFSET 1.
  • DISTINCT collapses tied salaries so ranks are correct.
  • DENSE_RANK (MySQL 8.0+) ranks salaries and reads clearly.
  • A subquery with MAX handles the second highest neatly.

Next, group and rank data with GROUP BY and HAVING, or remove duplicates in deleting duplicate rows.

Frequently Asked Questions

How do I find the Nth highest salary in MySQL?
A common way is SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET N-1;. Sorting descending and skipping N-1 rows lands on the Nth highest. Use DISTINCT so equal salaries do not create duplicates.
How do I find the second highest salary?
Use LIMIT with an offset of 1: SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;. This skips the top salary and returns the next distinct one, which is the second highest.
Why use DISTINCT when finding the Nth highest salary?
Without DISTINCT, if several employees share the top salary, they occupy multiple rows and the offset lands on the wrong value. DISTINCT collapses equal salaries so the Nth highest is a distinct salary, not a duplicate.
What is the DENSE_RANK method?
DENSE_RANK() assigns a rank to each salary, giving equal salaries the same rank with no gaps. You then filter WHERE rank = N to get the Nth highest salary. It is available in MySQL 8.0 and is very clear.
What is the difference between LIMIT OFFSET and DENSE_RANK?
LIMIT OFFSET with DISTINCT is simple and works in all MySQL versions. DENSE_RANK is a window function (MySQL 8.0+) that ranks rows and reads more clearly for complex cases, but both correctly find the Nth highest salary.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicDelete Duplicate Rows Your progress: 140 / 151 (93%) · Lesson 141 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free