The nth highest salary is the salary earned by a given number of employees in a company. This can be used to determine how many employees have been hired and what their salaries are. The nth highest salary is also useful for determining which employees are earning the most money, so that you can reward them for their hard work.
There are two ways to compute the nth highest salary: using an array or using an ordered list. The ordered list approach is more straightforward and easier to understand, but it takes longer to compute than the array approach.
Nth Highest Salary
How to find Nth highest salary from a table.
ename | sal |
---|---|
C | 24500 |
D | 35000 |
E | 28500 |
F | 31500 |
One of the most common ways to solve this problem of finding the Nth maximum salary from the Employee table is by using the correlated subquery. This is a special type of subquery where the subquery depends upon the main query and execute for every row returned by the main query. It’s slow but it can solve problems which are difficult to solve otherwise. Let’s see the SQL query to find the Nth highest salary using the Correlated subquery.
SQL Query:
SELECT name, salary FROM #Employee e1 WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2 WHERE e2.salary > e1.salary)
for the 2nd maximum you can replace N with 2, and for 3rd maximum replace N with 3, here is the output:
2nd highest salary:
SELECT name, salary FROM #Employee e1 WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2 WHERE e2.salary > e1.salary)SELECT name, salary FROM #Employee e1 WHERE 2-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2 WHERE e2.salary > e1.salary) Result: name salary Peter 5000
3rd highest salary:
SELECT name, salary FROM #Employee e1 WHERE 3-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2 WHERE e2.salary > e1.salary) Result: name salary John 4000
Explanation :
The distinct keyword is there to deal with duplicate salaries in the table. In order to find the Nth highest salary, we are only considering unique salaries. The highest salary means no salary is higher than it, the Second highest means only one salary is higher than it, 3rd highest means two salaries are higher than it, similarly Nth highest salary means N-1 salaries are higher than it.
Pros :
1) The generic solution works in all databases including Oracle, MySQL, SQL SERVER, and PostgreSQL.
Cons :
1) Slow, because the inner query will run for every row processed by the outer query.
See SQL Puzzles and Answers book for more of such SQL queries for practicing and improving your SQL query skill.
The nth highest salary in SQL SERVER using TOP keyword
You can use the TOP keyword to find the Nth highest salary in SQL SERVER. This is also faster than the previous solution because here we are calculating Nth maximum salary without a subquery.
SELECT TOP 1 salary FROM ( SELECT DISTINCT TOP N salary FROM #Employee ORDER BY salary DESC ) AS temp ORDER BY salary
Explanation:
By default ORDER BY clause print rows in ascending order, since we need the highest salary at the top, we have used ORDER BY DESC, which will display salaries in descending order. Again DISTINCT is used to remove duplicates. The outer query will then pick the topmost salary, which would be your Nth highest salary.
And, if you like books and just learning these queries from the interview’s sake then I suggest you first read a good book on SQL like Head First SQL. It will help you to build your fundamentals.
3rd highest salary in SQL SERVER
SELECT TOP 1 salary FROM ( SELECT DISTINCT TOP 3 salary FROM #Employee ORDER BY salary DESC ) AS temp ORDER BY salary Result: salary 4000
Here is another example where we have used the TOP keyword to find the second highest salary in Microsoft SQL SERVER 2008.
Nth maximum salary in MySQL using LIMIT keyword
Similar to TOP, MySQL also supports a LIMIT keyword, which provides pagination capability. You can find the nth highest salary in MySQL without using subquery as shown below:
SELECT salary FROM Employee ORDER BY salary DESC LIMIT N-1, 1
2nd highest salary in MySQL without subquery:
SELECT salary FROM Employee ORDER BY salary DESC LIMIT 1,1 salary 5000
3rd highest salary in MySQL using LIMIT clause:
SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2,1 salary 4000
Nth highest salary in MySQL using LIMIT clause:
SELECT salary FROM Employee ORDER BY Salary DESC LIMIT n-1,1
Explanation :
The benefit of this approach is that it’s faster than a correlated query approach but its vendor dependent. This solution will only work in a MySQL database.
Nth highest salary in Oracle using ROW_NUMBER() function
SELECT * FROM ( SELECT e.*, ROW_NUMBER() OVER (ORDER BY salary DESC) rn FROM Employee e ) WHERE rn = N; /*N is the nth highest salary*/
Read more: https://javarevisited.blogspot.com/2016/01/4-ways-to-find-nth-highest-salary-in.html#ixzz7XGG5tOKk