Database / SQL
SQL to find 2nd highest salary in the Employee table.
One of the ways is to use correlated subquery to find the Nth highest salary as shown below.
SELECT name, salary FROM employee emp1 WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM Employee emp2 WHERE emp2.salary > emp1.salary)
In the above query, the 'N' may be replaced with 2 to find the second highest salary. Also for the top 2 salaries, replace the condition N-1= with N-1>=.
More Related questions...