SQL
All Top 50 SQL Interview Questions#31
Find the Second Highest Salary in SQL (Handling Ties & NULLs)
MediumAmazonInterview Question #31
Asked at AmazonGiven 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| id | salary |
|---|---|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
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.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.