🧠 Introduction
The “Second Highest Salary” problem is one of the most
popular SQL interview questions. It’s a deceptively simple task that tests a
candidate’s understanding of sorting, filtering, subqueries, and sometimes
window functions.
In this chapter, you will learn:
Let’s begin with a sample table and walk through multiple
ways to approach this problem—from the traditional subquery to advanced
solutions using ROW_NUMBER() and RANK().
🗃️ Sample Table:
Employees
Let’s use the following table:
employees
|
employee_id |
name |
salary |
|
1 |
Alice |
50000 |
|
2 |
Bob |
60000 |
|
3 |
Charlie |
55000 |
|
4 |
David |
60000 |
|
5 |
Eva |
70000 |
|
6 |
Frank |
75000 |
🔍 Problem Statement
Write a SQL query to find the second highest distinct salary
from the employees table.
✅ Approach 1: Subquery with LIMIT
and DISTINCT (MySQL-specific)
SELECT
DISTINCT salary
FROM
employees
ORDER
BY salary DESC
LIMIT
1 OFFSET 1;
✅ Output:
|
salary |
|
70000 |
⚠️ This method works well in MySQL,
but not in all SQL dialects (e.g., SQL Server).
✅ Approach 2: Subquery with MAX()
and < operator (Standard SQL)
SELECT
MAX(salary) AS second_highest_salary
FROM
employees
WHERE
salary < (
SELECT MAX(salary)
FROM employees
);
✅ Output:
|
second_highest_salary |
|
70000 |
✅ Works in almost every SQL
database, including PostgreSQL, Oracle, and SQL Server.
✅ Approach 3: Using DENSE_RANK()
(Handles Duplicate Salaries)
WITH
ranked_salaries AS (
SELECT salary,
DENSE_RANK() OVER (ORDER BY salary
DESC) AS rank
FROM employees
)
SELECT
salary
FROM
ranked_salaries
WHERE
rank = 2;
⚠️ Best choice when you want to
return all employees with the second highest salary.
🔄 Variation: Include
employee details
WITH
ranked_employees AS (
SELECT *,
DENSE_RANK() OVER (ORDER BY salary
DESC) AS rank
FROM employees
)
SELECT
employee_id, name, salary
FROM
ranked_employees
WHERE
rank = 2;
🔁 Bonus: Handling Ties
with RANK() vs ROW_NUMBER()
|
Function |
Handles Ties? |
Skips Ranks? |
|
DENSE_RANK() |
✅ Yes |
❌ No |
|
RANK() |
✅ Yes |
✅ Yes |
|
ROW_NUMBER() |
❌ No |
❌ No |
Example with RANK():
WITH
ranked AS (
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS
rnk
FROM employees
)
SELECT
name, salary
FROM
ranked
WHERE
rnk = 2;
💡 Edge Cases
⚙️ Performance Tips
💼 Real-World Applications
🧑💻
Practice Challenge
Given a table players (player_id, name, score), write a
query to find all players with the second highest score, including ties.
Bonus: Modify it to return the third highest score.
✅ Summary
Interviewers often ask about joins, groupings, window functions, subqueries, and ranking problems. These test both your understanding of SQL syntax and logic.
Focus on solving real-world problems, practice on platforms like LeetCode or HackerRank, and understand how SQL is used in business scenarios.
Understanding logic is more important. Interviewers prefer candidates who can reason through a problem over those who rely on memorized solutions.
Yes. Functions like ROW_NUMBER(), RANK(), and DENSE_RANK() often appear in intermediate to advanced interviews.
Use real datasets, explore open SQL challenges online, and build your own problems from business scenarios.
Absolutely. Using aliases and CTEs can make your queries cleaner and more readable, which is often appreciated in interviews.
Learn to use IS NULL, COALESCE(), and proper filtering logic to handle NULLs, as they can affect groupings and joins.
Yes. Inner joins, left joins, and subqueries are very common in interviews, especially when querying across multiple tables.
Yes. Explaining both brute-force and optimized approaches shows depth in understanding and problem-solving.
With consistent practice, most people can get proficient in 2–4 weeks, especially if they focus on real interview problems.
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)