🔥 Launch Offer: Interview Preparation Bundle @ ₹2,499 ₹9,999 • 75% OFF. Offer expires in 🔥 Launch Offer: Interview Preparation Bundle @ ₹2,499 ₹9,999 • 75% OFF. Offer expires in Interview Preparation Bundle @ ₹2,499 ₹9,999

00:00:00 Get Bundle
home / practice / second-highest-salary-sql
easy SQL

Second highest salary (SQL)

Table Employee(id, name, salary). Write a query that returns the second highest distinct salary. If only one distinct salary exists, return NULL.

Sample

salaries: 100, 200, 200, 300
result:   200

Hint

Use DISTINCT + ORDER BY salary DESC + LIMIT 1 OFFSET 1, or a subquery with MAX excluding the top salary.

Starter code

-- Employee(id INT, name VARCHAR, salary INT)
-- Return the second highest distinct salary

SELECT ...

Solution

-- Option A: LIMIT / OFFSET (MySQL / Postgres)
SELECT DISTINCT salary AS SecondHighestSalary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

-- Option B: subquery (works widely)
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);