I was solving SQL based question on leetcode – https://leetcode.com/problems/nth-highest-salary/submissions/1259886791/ (nth largest income) and another was – https://leetcode.com/problems/second-highest-salary/ (2nd highest income) so i was looking at the solution of 1st problem (nth largest income) and for any greater number for which the answer can’t be given it generated null as output where as in 2nd problem (2nd largest income) here limit is not useful because it’s not generating null for any invalid input or input greater than number of tuples in table but why is this happening.
I am also providing the solution i found from leetcode and please do try to explain me as I am new in SQL.
Solution for problem 1 (Nth highest salary) used-
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N = N-1;
RETURN (
# Write your MySQL query statement below.
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
limit N, 1
);
END
solution for problem 2 (2nd highest salary) used
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
limit 1, 1
butwhere i had expected the null value but it generated blank at that location