Find the third highest salary in a table.

To find the third highest salary in a table, you can use a simple ORDER BY and LIMIT clause in SQL.

Problem

Write a function to find the third highest salary in a table.
Input:
Table:
+----+--------+
| Id | Salary |
+----+--------+
| 1  | 1000   |
| 2  | 2000   |
| 3  | 3000   |
| 4  | 4000   |
+----+--------+
Output:
3000

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
/*

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

*/

SELECT Salary
FROM Employee
WHERE Salary IN (
    SELECT MAX(Salary)
    FROM Employee
    WHERE Salary NOT IN (
        SELECT MAX(Salary)
        FROM Employee
    )
)

A.I. Evaluation of the Solution

The candidate's solution is correct and uses an optimal approach.

Evaluated at: 2022-11-19 06:16:09