Second to One

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
junior
Asked in
Cisco

Problem

A compensation review is comparing pay bands across departments. For each department, find the second highest salary, counting everyone who shares a pay figure as a single level, and list the departments alphabetically.

Across every team, the pay figure that sits just shy of the summit.

Tables

employees

Columns: employee_id INTEGER, emp_name TEXT, department TEXT, salary REAL, manager_id INTEGER, hire_date TEXT, user_id INTEGER

employees: 10 of 200 rows
employee_idemp_namedepartmentsalarymanager_idhire_dateuser_id
1Alice ChenEngineering55731NULL2021-02-02100
2Bob MartinezProduct61462NULL2022-03-03197
3Carol WuDesign67193NULL2023-04-04294
4David KimMarketing72924NULL2024-05-05391
5Eve JohnsonSales78655NULL2025-06-06488
6Frank PatelHR84386NULL2026-07-07585
7Grace LeeFinance55117NULL2020-08-08682
8Hank BrownOperations60848NULL2021-09-09779
9Iris DavisData66579NULL2022-10-10876
10Jake WilsonLegal72310NULL2023-11-11973

Expected output

First 5 of 10 rows
departmentsecond_highest_salary
Data130439
Design127983
Engineering119591
Finance128597
HR135556

Worked solution and explanation

Why this problem exists in real interviews

This is the classic 'second highest salary' question wearing a per-group costume. The single-table version (one MAX below the overall MAX) is a warm-up everyone passes. The real skill being probed is whether you can do that same logic independently inside every department without letting one department's pay bleed into another's, and whether you handle ties: if two people earn the top salary, the 'second highest' is still the next distinct figure down, not the same number again. Conflate those and your pay bands are quietly wrong.


Break down the requirements

Step 1: Rank distinct salaries within each department

PARTITION BY department restarts the ranking per team, and ORDER BY salary DESC puts the top earners first. Use DENSE_RANK, not ROW_NUMBER: DENSE_RANK gives equal salaries the same rank, so the second distinct pay level is always salary_rank = 2 even when several people tie for the top.

Step 2: Keep the second level

Filter to salary_rank = 2. Because DENSE_RANK numbers distinct salary values, this is precisely 'the second highest pay figure in the department', which is what a comp review means by a pay band.

Step 3: Collapse the ties and sort

If multiple employees share that second salary, the rank filter returns one row each, so SELECT DISTINCT department, salary folds them into a single band per department. ORDER BY department gives the alphabetical listing the reviewers asked for.


The solution

Second highest salary per department
WITH salary_ranks AS (
  SELECT department,
         salary,
         DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
  FROM employees
)
SELECT DISTINCT department,
       salary AS second_highest_salary
FROM salary_ranks
WHERE salary_rank = 2
ORDER BY department

Common follow-up questions