Parag Bhingre
Parag Bhingre

Reputation: 850

how to fnd top 5 salary in MySql without using limit

Below query is not working in MySql.

SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

help me out.

Upvotes: 1

Views: 80

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

Try this:

SET @n := 0;
SELECT * FROM
(SELECT salary, @n := @n + 1 AS t1 FROM Employee ORDER BY salary DESC)
  AS t
WHERE t1 <= 5

Working DEMO

Upvotes: 5

Related Questions