177: Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThis creates a function getNthHighestSalary that takes an integer n and r
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis creates a function getNthHighestSalary that takes an integer n and returns the nth highest salary in the Employee table.\n\nThe function works by first subtracting 1 from the input n to account for the fact that the LIMIT clause in SQL is 0-indexed.\n\nIt then uses a subquery to select the DISTINCT Salary values from the Employee table, orders them in descending order using ORDER BY Salary DESC, skips the first n results using OFFSET n, and returns the first result using LIMIT 1.\n\nIf there are fewer than n distinct salaries in the table, the function will return NULL.\n\nYou can call the function by executing:\n```\nSELECT getNthHighestSalary(n);\n\n```\nwhere n is the integer input to the function.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nCREATE FUNCTION getNthHighestSalary(n INT) RETURNS INT\nBEGIN\n    SET n = n-1;\n    RETURN (\n        SELECT DISTINCT Salary FROM Employee\n        ORDER BY Salary DESC\n        LIMIT 1 OFFSET n\n    );\nEND\n\n```
['Database', 'MySQL']