Second highest salary in mysql

Prem Tiwari - - Mysql

In this tutorial i will explain a very common interview question get second highest salary in mysql. if you are going for the role at junior level developer in PHP OR Mysql database administration, then this post will very useful for you.

Most of the people learn it character by character, but few understand how this query will works. Below is the mysql syntax to get second highest records from database table.

Mysql Table Structure

Create a mysql table and insert below sample records.

———- ———- ———- ———-
7839       KING             5000            10
7902       FORD            3000            20
7788       SCOTT          3000            20
7566       JONES          2975             20
7698       BLAKE         2850            30
7782       CLARK         2450            10
7499       ALLEN         1600            30
7844       TURNER      1500            30
7934        MILLER      1300            10
7521        WARD        1250           30
7654        MARTIN     1250             30

Now copy below query and go to the SQL tabs of phpmyadmin and run the scripts. Below query will return the 2nd highest records from tables.

Here is another options using Database LIMIT keyword. Try following query for second highest salary using LIMIT keyword.

Above query will return the below results. Below is the second highest record from database table.

———-    ———-    ———-    ———-
7566         JONES      2975                 20

nth records from Database

Try the following mysql query to fetch nth records from database table.

About: Prem Tiwari

Prem Tiwari is the founder of and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies.

Tags: , , , ,