How do you find the second highest salary?

SQL SELECT, filtering and sorting · Freshers and experienced

Model answer

One way: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). Another: ORDER BY salary DESC LIMIT 1 OFFSET 1 over distinct salaries. A more general way uses DENSE_RANK() in a window function and filters rank = 2. This is a very common interview question.

Learn this topic

SELECT, FROM and WHERE: Your First Queries in the course SQL for Beginners: Learn Databases Step by Step.

Sign in to add your own answer.

More questions