SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
To find the second highest salary from a table, you can use different methods based on sub-queries, the ORDER BY
clause, or even database-specific functions. Here are some of the common methods:
You can retrieve the second highest salary by filtering out the maximum salary and then finding the maximum of what's left.
SELECT MAX(salary) FROM employees WHERE salary NOT IN (SELECT MAX(salary) FROM employees);
ORDER BY
and LIMIT
:If your RDBMS supports LIMIT
and OFFSET
(like MySQL, PostgreSQL), you can order the salaries in descending order and then skip the highest one.
SELECT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;
ORDER BY
with the ROWNUM
or ROW_NUMBER()
:In databases like Oracle, you can use the ROWNUM
pseudo-column or the ROW_NUMBER()
window function:
Using ROWNUM
:
SELECT salary FROM ( SELECT salary, ROWNUM as rank FROM ( SELECT DISTINCT salary FROM employees ORDER BY salary DESC ) WHERE ROWNUM <= 2 ) WHERE rank = 2;
Using ROW_NUMBER()
:
SELECT salary FROM ( SELECT salary, ROW_NUMBER() OVER (ORDER BY salary DESC) as rank FROM employees ) WHERE rank = 2;
Some databases have specific functions or methods to handle such use cases. For instance, in MySQL, you can use the DISTINCT
keyword with ORDER BY
and LIMIT
:
SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1, 1;
While all the above methods achieve the same result, the best method can vary depending on the specific database system, the volume of data, indexing, and other factors. When dealing with real-world scenarios, it's essential to consider performance and choose the most efficient method for your specific database setup.
SQL query to find second highest salary in a table:
ORDER BY
clause in descending order and limiting the result set to one row.SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
Finding the second highest salary using SQL:
SELECT salary AS second_highest_salary FROM employees ORDER BY salary DESC OFFSET 1 ROW FETCH NEXT 1 ROW ONLY;
SQL SELECT statement for second highest salary:
LIMIT
clause to restrict the result to the second row after ordering by salary.SELECT salary AS second_highest_salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;
How to retrieve the second highest salary in SQL:
SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
Finding the runner-up salary in SQL:
SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
Query to get the second highest salary in a database:
LIMIT
to get the second highest salary after ordering by salary in descending order.SELECT salary AS second_highest_salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;
Using ORDER BY and LIMIT for second highest salary in SQL:
SELECT salary AS second_highest_salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;
SQL subquery to find second highest salary:
SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
Ranking and partitioning for second highest salary in SQL:
RANK()
window function to rank salaries and selecting the second-ranked salary.SELECT salary AS second_highest_salary FROM ( SELECT salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees ) ranked_salaries WHERE salary_rank = 2;
SQL query to exclude highest salary and find the second highest:
NOT IN
to exclude the highest salary and then finding the maximum.SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary NOT IN (SELECT MAX(salary) FROM employees);