SQL
Lab
Amazon Easy Ranking Classic

Second
Highest
Salary

Find the second highest distinct salary from the employee table.

Table Schema

Inspect
Table

interactive
ColumnTypeDescription
employee_idintegerUnique employee identifier.
salaryintegerEmployee salary.

Sample Data

Input
Output

Sample Input: employees
employee_idsalary
11000
22000
33000
43000
51500
62500
Expected Output
second_highest_salary
2500

SQL Editor

Run
Query

postgresql
Waiting for query

second_highest_salary
2500

Hints

Unlock
Clues

Hint 01: Use DISTINCT salary so repeated salaries are counted once.
Hint 02: Sort salaries in descending order.
Hint 03: OFFSET 1 LIMIT 1 returns the second highest distinct salary.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT
  salary AS second_highest_salary
FROM (
  SELECT DISTINCT
    salary
  FROM employees
) AS distinct_salaries
ORDER BY salary DESC
OFFSET 1
LIMIT 1;

Explanation

Step By
Step

01

Select distinct salaries so duplicate values do not affect the ranking.

02

Order those salaries from highest to lowest.

03

Skip the highest salary and return the next one as second_highest_salary.

Related Questions

Keep
Solving