Find Second Highest Salary in MySql

Write a query to find second highest salary inMySql . This question is mostly asked in an interviews and it’s also a bit tricky. In this tutorial, We are going to learn three easiest ways to write a MySql query to find second highest salary.

Find Second Highest Salary in MySql

Find Second Highest Salary in MySql

 

Suppose we have one table Employee. In this table, we have following record of an employee and each employee has a different salary. As per table, the highest salary of an employee is 12000 and second highest salary is 10000. How to write a query to find second highest salary of an employee.

 

Difference between inner join and left join

MySql Interview Questions

Let’s first write a MySql query to find the maximum salary. In MySql, we can use MAX() function to find the highest value of a selected column.

How to Find Second Highest Salary in Mysql

If we look at the employee table we know emp_id 1 is getting a second highest salary. Now lets’s write a MySql query to find second highest salary.

First Method : Using sub-query

In this approach, we have used sub-query to find max salary first then excluded this value using not in to find second highest value.

Second Method – Using Limit

Using limit to find the second highest salary. In this query, we are using a Distinct keyword to filter duplicate records.

Third Method to find second highest  salary in Mysql

Conclusion

In this post, I have used some common approaches to find second highest salary in MySql. If you know some other ways to find second highest salary you can let us know through your comments.

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.