SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
To find the Nth highest salary from a table, there are several approaches you can use, depending on the specific SQL database system you are using. Here are some methods using standard SQL:
Assuming the table is named employees
and the salary column is salary
.
LIMIT
clause (works for databases that support the LIMIT
clause, such as MySQL):SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET N-1; -- replace N with the desired value
ROW_NUMBER()
window function (commonly supported in SQL Server, Oracle, PostgreSQL, etc.):WITH RankedSalaries AS ( SELECT salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank FROM (SELECT DISTINCT salary FROM employees) AS SubQuery ) SELECT salary FROM RankedSalaries WHERE rank = N; -- replace N with the desired value
SELECT salary FROM employees e1 WHERE N-1 = (SELECT COUNT(DISTINCT e2.salary) FROM employees e2 WHERE e2.salary > e1.salary);
Choose the method that best fits your database system and your specific use case. Remember to replace N with the desired rank to get the Nth highest salary.
Finding the Second Highest Salary in SQL:
SELECT MAX(Salary) AS SecondHighestSalary FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees);
How to Get the Third Highest Salary Using SQL:
SELECT MAX(Salary) AS ThirdHighestSalary FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees));
SQL Query to Find the Top N Salaries from a Table:
SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 5; -- Change this to the desired value of N
Using ORDER BY and LIMIT for Nth Highest Salary in SQL:
SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 3; -- Change this to the desired value of N
Ranking and Partitioning for Nth Highest Salary in SQL:
SELECT Salary FROM ( SELECT Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS SalaryRank FROM Employees ) AS RankedSalaries WHERE SalaryRank = 3; -- Change this to the desired value of N
Finding the Fourth Highest Salary in SQL Database:
SELECT MAX(Salary) AS FourthHighestSalary FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees)));
Selecting Top N Salaries from a Table in SQL:
SELECT EmployeeID, Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY; -- Change this to the desired value of N
Using DENSE_RANK() for Nth Highest Salary in SQL:
SELECT Salary FROM ( SELECT Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS SalaryRank FROM Employees ) AS RankedSalaries WHERE SalaryRank = 5; -- Change this to the desired value of N
SQL Query to Find the Fifth Highest Salary in a Table:
SELECT MAX(Salary) AS FifthHighestSalary FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees))));