dense_rank() over() solution
Intuition\nIt is basically ordering the employees by salary in descending order and picking nth employee\n\n# Approach\nWe can use limit but it will give alll t
# Intuition\nIt is basically ordering the employees by salary in descending order and picking nth employee\n\n# Approach\nWe can use limit but it will give alll the record till nth and we don\'t need that. So we use window function that is dense_Rank to give rank to every row based on salary and since rank can give non-consecutive ranks to rows so we choose dense_Rank and return the unique salary since amny rows can have same rank but it is assured that these row eill have same salary\n\n# Complexity\n- Time complexity:\nSImce the dense_rank is sorting and assigning rank so for m total rows it takes m(log(m)) time ans after that we select rank= n so we it takes m time as well to find that and our T.C. = O(n(log(n))) + O(n) = O(n)\n\n- Space complexity:\nSorting takes O(m) space and storing the intermediate result to be used by the outer query again takes O(m) and finding takes O(1) so => O(m) +O(m) + O(1) => O(m);\n\n# Code\n```\nCREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT\nBEGIN\n  RETURN (\n      # Write your MySQL query statement below.\n      SELECT distinct(sub.salary) from\n      (\n      select salary, dense_rank() over (order by salary DESC) as r\n        from employee) as sub\n        where sub.r = N\n  );\nEND\n```
['MySQL']