Find Second Highest Salary

TCS NQT Coding Easy Go Ad-Free - ₹20/mo

Write a SQL query to find the second-highest salary from an employee table. The table has a column named 'salary' containing integer values. Assume there are at least two distinct salary values in the table.

Examples:

Input: Employee table with salaries: [100, 200, 300]

Output: 200

Explanation: The distinct salaries are 100, 200, and 300. The second-highest salary is 200.

Input: Employee table with salaries: [500, 500, 400]

Output: 400

Input: Employee table with salaries: [1000, 800, 800, 600]

Output: 800

Constraints

  • Salaries are positive integers.
  • The table contains at least two distinct salary values.
  • The number of rows can be large, so the query should be optimized for efficiency.