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 |
+----+---------+
| 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 TOP 1 Amount FROM
( SELECT TOP (2) Amount FROM
tbl_TeacherPay
ORDER BY AmountDESC)
AS tbl_TeacherPay order by Amount asc
Result
+----+---------+
| Id | Amount |
+----+---------+
| 1 | 1200 |
| Id | Amount |
+----+---------+
| 1 | 1200 |
+----+---------+
No comments:
Post a Comment