How to Find First, Second, Third, Nth Highest Salary in MySQL

To select or get first, second, third, and nth highest salary in mysql; In this tutorial, i am going to show you how to select or get first, second, and third, … nth highest salary from the MySQL database table.

MySQL Queries to Find the First, Second, Third, …nTh Highest Salary

Use the following mysql queries to find first, second, third and nth highest salary from database table; as follows:

  • 1 – MySQL Query To Find First Higheset Salary
  • 2 – Find Second Highest/max salary in MySQL without limit using sub query and IN clause
  • 3 – Using subquery and < operator instead of IN clause to find second highest salary
  • 4 – To find second highest salary Using the LIMIT clause in query
  • 5 – If multiple employees have the same salary to find second highest salary
  • 6 – MySQL query to get the third highest salary
  • 7 – How to find Nth highest salary from a table

1 – MySQL Query To Find First Higheset Salary

Use the following MySQL query to find the first highest salary from MySQL database table;

SELECT name, MAX(salary) as salary FROM employee 

2 – Find Second Highest/max salary in MySQL without limit using sub query and IN clause

Use the following MySQL query to find the second highest salary from MySQL database table;

 SELECT MAX(salary) 
FROM employees
WHERE salary NOT IN ( SELECT Max(salary) FROM employees);

3 – Using subquery and < operator instead of IN clause to find second highest salary

Using subquery and < operator to find the second highest salary from MySQL database table;

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

4 – To find second highest salary Using the LIMIT clause in query

Using limit clause to find the first highest salary from MySQL database table;

SELECT salary 
FROM (SELECT salary FROM employees ORDER BY salary DESC LIMIT 2) AS Emp ORDER BY salary LIMIT 1;

5 – If multiple employees have the same salary to find second highest salary

Use the following query, If multiple employees have the same salary to find second highest salary;

SELECT * FROM employee 
WHERE salary= (SELECT DISTINCT(salary) 
FROM employee ORDER BY salary LIMIT 3,1);

6 – MySQL query to get the third highest salary

Use the following query to find third highest salary in MySQL database;

SELECT * FROM employee ORDER BY salary DESC LIMIT 2,1;

7 – How to find Nth highest salary from a table

Use the following query to find nth highest salary in MySQL database;

SELECT salary FROM Employee  ORDER BY Salary DESC LIMIT n-1,1

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *