
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Nth Highest Salary in SQL
In this article, we will write an SQL(Structured Query Language) query to find the Nth highest salary from a database table. We can obtain the 2nd highest salary, 3rd highest salary, and so on. Certainly, This is the most commonly asked question in an interview. There are different methods to achieve this, we will understand every technique in detail.
Problem Statement
Assume we have a table named employees with 3 columns namely, EmployeeId, EmployeeName and, EmployeeSalary as shown below ?
EmployeeId | EmployeeName | EmployeeSalary |
---|---|---|
10001 | Mahesh | 33000 |
10002 | John | 35000 |
10003 | Abdul | 34500 |
10004 | Raman | 38000 |
Given task is to find out any Nth order highest salary request like 2nd highest salary is 35000, 3rd, and so on.
Data Creation
Let's create the table using the CREATE query ?
CREATE TABLE Employees ( EmployeeId INT PRIMARY KEY, EmployeeName VARCHAR(255), EmployeeSalary DECIMAL(10, 2) );
Now, we will provide the records mentioned above using the INSERT statement
INSERT INTO Employees (EmployeeId, EmployeeName, EmployeeSalary) VALUES (10001, 'Mahesh', 33000), (10002, 'John', 35000), (10003, 'Abdul', 34500), (10004, 'Raman', 38000);
Find Nth Highest Salary in SQL
We can find the Nth highest salary in SQL
- Using DISTINCT and LIMIT clauses
- Using Correlated Subquery
- Using DENSE_RANK() function
Using DISTINCT and LIMIT clauses
Let's understand the query creation approach
- Find out DISTINCT records of employeeSalary
- Use ORDERBY with descending order to arrange the highest salary on 1st row
- OFFSET to N-1 skips first N-1 records from sorted result
- Finally, LIMIT 1 to fetch a unique Nth record
Syntax
Following is the query to find the Nth highest salary in SQL where N representing the Nth order of salary ?
SELECT DISTINCT EmployeeSalary FROM Employees ORDER BY EmployeeSalary DESC LIMIT 1 OFFSET N-1;
Example
Following is the SQL query for 2nd highest salary
SELECT DISTINCT EmployeeSalary FROM Employees ORDER BY EmployeeSalary DESC LIMIT 1 OFFSET 1;
Similarly, Following query retrieves 3rd highest salary
SELECT DISTINCT EmployeeSalary FROM Employees ORDER BY EmployeeSalary DESC LIMIT 1 OFFSET 2;
Using Correlated Subquery
Following is the approach for query creation
- Create a sub-query to count no. of unique salaries greater than the specified salary, i.e., the N-1 row has a higher salary than that specified salary.
- Combine with a main query using the WHERE clause to match N-1 counts of higher salary.
Syntax
Following is the query to find the Nth highest salary in SQL using the correlated subquery
SELECT EmployeeSalary FROM Employees AS e1 WHERE N-1 = ( SELECT COUNT(DISTINCT EmployeeSalary) FROM Employees AS e2 WHERE e2.EmployeeSalary > e1.EmployeeSalary );
Example
Following is the SQL correlated subquery query for 2nd highest salary
SELECT EmployeeSalary FROM Employees AS e1 WHERE 1 = ( SELECT COUNT(DISTINCT EmployeeSalary) FROM Employees AS e2 WHERE e2.EmployeeSalary > e1.EmployeeSalary );
Similarly, following is the correlated SQL subquery for 3rd highest salary
SELECT EmployeeSalary FROM Employees AS e1 WHERE 2 = ( SELECT COUNT(DISTINCT EmployeeSalary) FROM Employees AS e2 WHERE e2.EmployeeSalary > e1.EmployeeSalary );
Using DENSE_RANK() function
Approach for query creation -
- Use ORDERBY with descending order to arrange the highest salary on 1st row
- Identify the rank of each row which was ordered using the DENSE_RANK() function
- Apply WHERE condition to fetch Nth highest salary
DENSE_RANK() function:
The output of the function is rank, which is calculated as
rank of a specific row = 1 + number of distinct rank values that appear before that specific row
Hence, the DENSE_RANK function allocates ranks to rows, assigning 1 to the first row and so on.
The record duplicate is already handled by the DENSE_RANK function because the function identifies rank with distinct records.
Raw Query
SELECT EmployeeSalary FROM ( SELECT EmployeeSalary, DENSE_RANK() OVER (ORDER BY EmployeeSalary DESC) AS salary_rank FROM Employees ) AS ranked_salaries WHERE salary_rank = N;
Example
SELECT EmployeeSalary FROM ( SELECT EmployeeSalary, DENSE_RANK() OVER (ORDER BY EmployeeSalary DESC) AS salary_rank FROM Employees ) AS ranked_salaries WHERE salary_rank = 2;
SQL query for 3rd highest salary
SELECT EmployeeSalary FROM ( SELECT EmployeeSalary, DENSE_RANK() OVER (ORDER BY EmployeeSalary DESC) AS salary_rank FROM Employees ) AS ranked_salaries WHERE salary_rank = 3;
Conclusion
Any of them techniques can be used according to specific requirements but DENSE_RANK() function is suggested to obtain better performance in a larger number of records. All the above methods are well handled with the condition of duplicate records hence you always get correct results although there are duplicate salary amounts of different Employees.