MySql Query to Find Second Highest Salary

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

MySql Query to Find Second Highest Salary

MySql Query to Find Second Highest Salary

For explanation, Let’s first create one dummy table employee. In this table, let’s populate some records of an employee and each employee has a different salary.

 

Subscribe Our Tutorials

Get Latest Updates on Facebook

Difference between inner join and left join

MySql Interview Questions

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

MySql Query to Find Second Highest Salary

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 of an employee from an employee table.

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.

WebRewrite

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.