Sql query to find second highest salary in mysql

3 minutes read

How to get second highest or nth maximum salary of an Employee table is one of the most frequently asked Mysql/SQL interview question.

PHP Mysql

PHP Mysql Query

Suppose we have the following simple database table called Employee that has 3 columns named Employee ID,Employee Name and Salary:

 Employee

##########################################

Employee_ID —– Employee_Name ——- Salary
1: —————– Avanish——— ——- 1000
2: —————– Abhishek——- ——- 1500
3:———— —– Tathagat——– ——- 700
4:———— —– Akarsh —————– 900
5: —————– Jagdish —————- 1700

##########################################

1:Now Query to get the highest salary:

2:Now Query to get the Second highest salary:

Make the the query selection in descending order with salary and use limit.

Why LIMIT 1 , 1  ? =>  first 1 is for the starting point and second one is for the total record to be fetch. Like currently we are trying to get second highest salary so start point will 1( index starts from 0 ( zero ) ) ,and we need to get only second highest so need to select only one record.

3:Now find the nth highest salary :

 

 

 

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

If you looking for “How to get the number of rows about to get deleted.Please try small tricks to avoid unwanted data deletion. You can use SELECT  to get the number of rows about to get deleted. PHP Mysql Query By this query we check  the number of the delete...
Below Mysql query is very useful to Get list of column name from table with its datatype. You can get list of all the tables name and all the fields count with help of this query. PHP Mysql Query In below query just change <–YoursDatabaseName–> to your d...
Frequently asked basic and advanced MySql Queries Interview Questions and Answers. PHP Mysql Query 1. What is MySQL?  MySQL is an open source RDBMS which is built, supported and distributed by MySQL (now acquired by Oracle). 2. What is the Differentiate betwee...