Nth Highest Salary In Sql

In SQL, the Nth highest salary is a query that returns the nth highest salary from the table of salaries.

For example, if you want to find the third-highest salary in a table of salaries, you would use this query: SELECT * FROM employees WHERE salary >= (SELECT MAX(salary) FROM employees) – 1 ORDER BY salary DESC.

This query will give you every employee’s name and their salary, along with their rank as it relates to other salaries in the table.

Nth Highest Salary In Sql

Finding Nth highest salary in a table is the most common question asked in interviews. Here is a way to do this task using dense_rank() function. 

Consider the following table: 
Employee 

ename sal 
A23000
B31000
C24500
D35000
E28500
F31500
G39800
H51000
I39800

Query : 

select * from(
select ename, sal, dense_rank() 
over(order by sal desc)r from Employee) 
where r=&n;

To find to the 2nd highest sal set n = 2
To find 3rd highest sal set n = 3 and so on.

Output : 
 

DENSE_RANK : 

  1. DENSE_RANK computes the rank of a row in an ordered group of rows and returns the rank as a NUMBER. The ranks are consecutive integers beginning with 1.
  2. This function accepts arguments as any numeric data type and returns NUMBER.
  3. As an analytic function, DENSE_RANK computes the rank of each row returned from a query with respect to the other rows, based on the values of the value_exprs in the order_by_clause.
  4. In the above query the rank is returned based on sal of the employee table. In case of tie, it assigns equal rank to all the rows. 
     

Alternate Solution :
—————————————————————————————————————————————————————————————————————– 

CREATE TABLE `Employee` ( 
`ENAME` varchar(225) COLLATE utf8_unicode_ci NOT NULL, 
`SAL` bigint(20) unsigned NOT NULL, 
PRIMARY KEY (`ENAME`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

————————————————————————————————————————————————————- 

6th highest
mysql> select * from ((select * from Employee 
       ORDER BY `sal` DESC limit 6 ) AS T) 
       ORDER BY T.`sal` ASC limit 1;
Alternate use of Limit:
select * from Employee ORDER BY `sal` ASC limit 5,1; // will return 6th highest 


+-------+-----+
| ENAME | SAL |
+-------+-----+
| B     | 300 |
+-------+-----+
1 row in set (0.00 sec) 

——————————————————————————————————————————————————– 

mysql> select * from Employee;
+-------+-----+
| ENAME | SAL |
+-------+-----+
| A     | 100 |
| B     | 300 |
| C     | 200 |
| D     | 500 |
| F     | 400 |
| G     | 600 |
| H     | 700 |
| I     | 800 |
+-------+-----+
8 rows in set (0.00 sec) 

Thanks to Vijay for suggesting this alternate solution. 
 

Alternate Solution –
Suppose the task is to find the employee with the Nth highest salary from the above table. We can do this as follows: 

  1. Find the employees with top N distinct salaries.
  2. Find the lowest salary among the salaries fetched by the above query, this will give us the Nth highest salary.
  3. Find the details of the employee whose salary is the lowest salary fetched by the above query.

Query:  

SELECT * FROM Employee WHERE sal = 
         (
            SELECT MIN(sal) FROM Employee 
            WHERE  sal IN (
                                 SELECT DISTINCT TOP N
                                     sal FROM Employee 
                                         ORDER BY sal DESC
                             )
        )

The above query will fetch the details of the employee with the Nth highest salary. Let us see how: 

  • Consider N = 4.
  • Starting with the most inner query, the query: “SELECT DISTINCT TOP sal FROM Employee ORDER BY sal DESC” will produce the below result: 
     
51000
39800
35000
31500
  • The next outer query is: “SELECT MIN(sal) FROM Employee WHERE sal IN ( Result_Set_of_Previous_Query )“. This will return the below result:
31500
  • You can see that the above returned result is the required 4th highest salary.
  • Next is the most outer query, which is: “SELECT * FROM Employee WHERE sal = Result_of_Previous_Query“. This query will return the details of employees with 4th highest salary. 
     
________________________
ename             sal
________________________
  F        |     31500
           |
________________________

Another Solution –
Here N = nth Highest Salary eg. 3rd Highest salary : N=3 .

SELECT ename,sal from Employee e1 where 
        N-1 = (SELECT COUNT(DISTINCT sal)from Employee e2 where e2.sal > e1.sal) 

Solution using Limit : 

    Select Salary from table_name order by Salary DESC limit n-1,1;

        Here we are ordering our salary in descending order so we will get highest salary first and then subsequently lower salaries. 

        Limit clause has two components, First component is to skip  number of rows from top and second component is display number of rows we want. 

   let us see with an example :

    To find 4th Highest salary query will be :

 Select Salary from table_name order by Salary DESC limit 3,1;

   Here  we are skipping 3 rows from Top and returning only 1 row after skipping .

 You can also find names of employees having Nth Highest Salary 

Select Emp_name from table_name where Salary =( Select Salary from table_name order by Salary DESC limit n-1,1);

There can be another question like find Nth Lowest Salary . In order to that , just reverse order using ASC ( if you don’t specify by default column will be ordered in ascending order).

 Select Salary from table_name order by Salary limit n-1,1;

Leave a Reply