Find the Second Highest Salary
Return the employees who earn the second-highest salary, handling ties correctly.
The challenge runs on the SQLabHub practice database (SQLite) and uses the employees table. You can solve it in the browser, with nothing to install.
The task
Find employees with the second highest salary. Handle ties correctly.
A correct answer returns 1 row with 5 columns: first_name, last_name, department, title, salary.
Tables used in this challenge
employees 120 rows
id INTEGER (PK) · first_name TEXT · last_name TEXT · email TEXT · department TEXT · department_id INTEGER · title TEXT · salary REAL · hire_date TEXT · manager_id INTEGER · city TEXT · status TEXT
| id | first_name | last_name | department | department_id | title | salary | hire_date | manager_id | city | status | |
|---|---|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| first_name | last_name | department | title | salary |
|---|---|---|---|---|
The complete result: 1 row. This is the output of the model solution on the practice database.
What you practise
- Choosing exactly which columns a query returns
- Filtering rows with a WHERE condition
- Removing duplicate rows with DISTINCT
- Using the result of one query inside another
- Sorting the result with ORDER BY
- Returning only the first rows with LIMIT
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Use a subquery with OFFSET 1 on DISTINCT salaries
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
employeestable. - Filter in
WHERE: keep the rows wheresalaryequals the value returned by a subquery. - The subquery runs once and returns a single value. It works like this:
- Read from the
employeestable. - Return only the column
salary. DISTINCTremoves duplicate rows, so each value appears once.- Sort the result by
salarydescending. - Skip the first row with
OFFSETand return the next one.
- Read from the
- Return only the columns
first_name,last_name,department,titleandsalary.
Model solution
Checking your session…
What to watch for
The subquery takes the distinct salaries, skips the highest and returns the next. The outer query then returns everyone on that salary, so a tie for second place gives several rows. DENSE_RANK() = 2 is the window-function version of the same answer.
- Subquery. A subquery compared with =, > or < must return exactly one row and one column. If it can return several rows, use IN or an aggregate.
- DISTINCT. DISTINCT works on the whole selected row. Adding one more column to the SELECT list can bring the duplicates back.
- LIMIT. LIMIT without ORDER BY returns an arbitrary set of rows. LIMIT is also dialect-specific: SQL Server uses TOP and Oracle uses FETCH FIRST.
Related SQL challenges
- Find the Second Highest Salary in Each DepartmentSimilar levelHard
- Find Employees Earning Above the Median SalarySimilar levelHard
- Find the Second Highest Order Total with LIMIT and OFFSETEasierMedium
- Find Who Placed the Largest Order with a SubqueryEasierMedium
- Find Employees Earning Above the Company AverageEasierMedium
Keep learning
- Lesson: SQL Subqueries
- Lesson: SELECT DISTINCT
- Lesson: LIMIT / TOP / FETCH
- Lesson: ORDER BY
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database