Write a SQL query to get the second highest salary from the
For example, given the above Employee table, the query should return
200 as the second highest salary. If there is no second highest salary, then the query should return
Difficulty Level – Easy
SELECT MAX(salary) as SecondHighestSalary FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee)
In the subquery, we first select the maximum salary which will give us 300. Then in the outer query, we are again selecting the maximum salary which is less than 300 using the WHERE clause, this will give us 200, which is the second-highest salary in this table.
To learn more about a general query which can find any highest salary from a table, then read this previous post – https://lifewithdata.com/2021/08/01/amazon-sql-interview-questions-leetcode-177-nth-highest-salary/