Fastest solution without using order by or declaring variables

  • 1

    Often this question would be followed by solving the same problem without order by, rank, or declaring variables. Below are two accepted solutions for that problem. However, both of them are a lot slower than other accepted answers for this question, I think because the faster questions use order by -- what is the fastest way to write this query without order by?

    solution 1, correlated subquery, 1311 ms:

    select distinct Salary from Employee e1 
        where N = (select count(distinct Salary)
        from Employee e2
        where e1.Salary <= e2.Salary)

    solution 2, self join - 1329 ms:

         select salary from
          ((select N as rank) rank
          left join
          (select a.salary, count(distinct b.salary) as rank
          from Employee a
          join Employee b
          where a.salary <= b.salary
          group by a.salary) salary_rank
          on rank.rank = salary_rank.rank)

Log in to reply

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