Example
Write a SQL Query to display the Employee name, taking the second-highest Salary.
Let us decompose the given Query:
- To find the Employee's name who is having the second-highest Salary, we need to find the second highest Salary.
- So, we must firstly find the highest Salary, which can be done with the max() aggregate function.
The following Query returns the maximum Salary from the table EMPLOYEE.
SELECT max(Salary) from Employee; -- 60,000
The following Query excludes the highest Salary using the <>(NOT EQUAL TO) operator and gives us the Second Highest Salary.
SELECT max(Salary) from Employee
WHERE Salary <> (SELECT max(Salary) from Employee); -- 50,000
With the help of the above queries we can find the Employee who is taking the second-highest Salary:
SELECT EmpName from Employee
WHERE Salary IN (SELECT max(Salary) from Employee
WHERE Salary <> (SELECT max(Salary) from Employee));
Output:

Frequently Asked Questions
Is it possible to add, modify, and drop columns in a single ALTER TABLE command?
Yes, you can combine multiple operations in a single ALTER TABLE command. This is efficient as it reduces the number of times the table needs to be locked for changes, but ensure you order operations carefully to avoid conflicts.
What is the difference between the HAVING Clause and WHERE Clause in SQL?
WHERE Clause filters records from a table based on the condition supplied; in contrast, the HAVING Clause filters records from groups based on the condition provided.
Is it possible to undo a DELETE operation?
Once a DELETE operation is executed, it cannot be undone unless you have a backup of the data or if your database supports transactional operations where you can roll back the transaction before it's committed.
Conclusion
In this article, we've learned how to get Second Highest Salary in SQL. Understanding how to accurately use this command is crucial for maintaining the integrity and relevance of your database.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.