Tuesday, 6 September 2016


Second Highest Salary in MySQL and SQL Server

Write a SQL query to get the second highest salary from the table.
+----+---------+
| Id | Amount  |
+----+---------+
| 1  | 1100    |
| 2  | 1200    |
| 3  | 1300    |
+----+---------+

For example, given the above a table , the second highest salary is 1200. If there is no second highest salary, then the query should return NULL.
suppose table name is tbl_TeacherPay


So Query is


SELECT TOPAmount FROM
( SELECT TOP (2) Amount FROM tbl_TeacherPay
ORDER BY AmountDESC)
AS tbl_TeacherPay order by Amount asc


Result

+----+---------+
| Id | Amount  |
+----+---------+
| 1  | 1200    |
+----+---------+


No comments:

Post a Comment