Find the Second Highest Salary in a SQL Database

To find the second highest salary from a SQL database, you can use a subquery. This subquery will return the salary of the employee with the second highest salary.

Problem

Write a SQL query to find the second highest salary from the employees table.
Example input:
| salary |
|--------|
|   1000 |
|   2000 |
|   3000 |
|   4000 |
Example output:
| salary |
|--------|
|   3000 |

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;

/*

The above query is optimal because it uses the ORDER BY clause to sort the salaries in descending order, and then uses the LIMIT and OFFSET clauses to return the second highest salary.

*/

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a good understanding of how to use the ORDER BY, LIMIT, and OFFSET clauses in SQL to solve the problem.

Evaluated at: 2022-11-19 02:16:29