Table Schema
Inspect
Table
| Column | Type | Description |
|---|---|---|
| employee_id | integer | Unique employee identifier. |
| salary | integer | Employee salary. |
Find the second highest distinct salary from the employee table.
Table Schema
| Column | Type | Description |
|---|---|---|
| employee_id | integer | Unique employee identifier. |
| salary | integer | Employee salary. |
Sample Data
| employee_id | salary |
|---|---|
| 1 | 1000 |
| 2 | 2000 |
| 3 | 3000 |
| 4 | 3000 |
| 5 | 1500 |
| 6 | 2500 |
| second_highest_salary |
|---|
| 2500 |
SQL Editor
| second_highest_salary |
|---|
| 2500 |
Hints
Solution
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
Select distinct salaries so duplicate values do not affect the ranking.
Order those salaries from highest to lowest.
Skip the highest salary and return the next one as second_highest_salary.
Related Questions