SQLSQL
#31

Find the Second Highest Salary in SQL (Handling Ties & NULLs)

MediumAmazon
Interview Question #31
Asked at Amazon

Given an `employee` table with `id` and `salary`, write a SQL query to find the second highest distinct salary. If there is no second highest salary (e.g. only 1 employee or all salaries are equal), the query should return `NULL`.

Input Table: employee
3 rows preview
idsalary
1100
2200
3300
Expected Output Structure1 rows
SecondHighestSalary
200
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us