Simple answer by assigning ranks

  • 0
      SELECT if(count(*) = 0, null, T.`Salary`)
      FROM (
          SELECT E.`Salary`, @currRank := @currRank + 1 AS `Rank`
          FROM (
            SELECT e.`Salary`
            FROM Employee e
            GROUP BY e.`Salary`
            ORDER BY e.`Salary` DESC
          ) E, (SELECT @currRank := 0) r
      ) T
      WHERE T.`Rank` = N

    Works by assigning a rank to every distinct salary. Let me know if there's anything I can do to improve it.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.