Help the world stop coronavirus! Stay home!

Prev Next

Hibernate / 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>=.

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Show more question and Answers...

Maven

Comments & Discussions