Find the Second Highest Salary in an Employees Table

This technical problem asks the reader to write a SQL query to find the second highest salary from a given table of employees. An example table is provided, along with the expected output.

Problem

Write a SQL query to find the second highest salary from the employees table.
Example input:
+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
Example output:
+--------+
| Salary |
+--------+
| 200    |
+--------+

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT MAX(Salary) AS Salary
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee)

/*

The solution above is optimal because it uses a subquery to find the maximum salary and then uses that value to find the second highest salary.

*/

A.I. Evaluation of the Solution

The solution above is optimal because it uses a subquery to find the maximum salary and then uses that value to find the second highest salary.

Evaluated at: 2022-11-26 06:16:18