Write a query to find the second highest salary from an Employee table.

Four ways to find the second highest salary in SQL (MAX subquery, DENSE_RANK, LIMIT/OFFSET and TOP), how to handle ties and missing rows, and how to get the Nth highest.

0 0
Read Time:2 Minute, 8 Second

Almost every SQL round asks this. Give one working query, then show you understand ties, the case where there is no second salary, and how to get the Nth highest.

Assume this table:

CREATE TABLE Employee (
    id     INT PRIMARY KEY,
    name   VARCHAR(50),
    salary INT
);
-- salaries: 90000, 75000, 75000, 60000

The second highest distinct salary here is 75000.

1. Subquery with MAX (works everywhere)

SELECT MAX(salary) AS second_highest
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);

The inner query finds 90000; the outer query takes the largest salary below it. If there is only one distinct salary it returns NULL rather than no row, which is usually what the interviewer wants.

2. DENSE_RANK (best for the Nth highest)

SELECT DISTINCT salary
FROM (
    SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
    FROM Employee
) ranked
WHERE rnk = 2;

Change 2 to N for the Nth highest. To return the employees who earn it, select name, salary instead and drop DISTINCT.

3. LIMIT and OFFSET (MySQL, PostgreSQL)

SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

DISTINCT matters: without it, duplicate top salaries would make this return the top salary again. This returns no row (not NULL) when there is no second salary.

4. TOP (SQL Server)

SELECT TOP 1 salary
FROM (SELECT DISTINCT TOP 2 salary FROM Employee ORDER BY salary DESC) t
ORDER BY salary ASC;

ROW_NUMBER vs RANK vs DENSE_RANK

For salaries 90000, 75000, 75000, 60000:

salary  ROW_NUMBER  RANK  DENSE_RANK
90000   1           1     1
75000   2           2     2
75000   3           2     2
60000   4           4     3

ROW_NUMBER would wrongly call the second 75000 the third highest, and RANK skips 3, so asking for rank 3 returns nothing. DENSE_RANK gives the answer people usually mean by “Nth highest salary”.

Common mistakes

  • Using ORDER BY salary DESC LIMIT 1, 1 without DISTINCT.
  • Using ROW_NUMBER when salaries can tie.
  • Forgetting that NULL salaries are ignored by MAX but sort differently by database in ORDER BY.
  • Not saying which database the syntax is for.

Follow-up questions to expect

  • Find the highest salary in each department.
  • Find employees who earn more than their manager.
  • Write it without a subquery or window function (a self join with COUNT).

This answer was written by Debugger’s Space as practice material. It is one of the questions asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company’s hiring steps and practice questions on Debugger’s Space.

Previous post How does a HashMap work inside, and what changed in Java 8?
Next post Reverse a string without using a built-in reverse function.
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Close
0
Would love your thoughts, please comment.x
()
x