Nth Highest Salary

考察sql中函数使用,表内自连接排序,可以得到rank。
https://leetcode.com/problems/nth-highest-salary/

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      SELECT DISTINCT A.Salary FROM 
      (SELECT E2.Id,E2.Salary,COUNT(1) AS RANK FROM
      (SELECT DISTINCT Salary FROM Employee) E1
      INNER JOIN Employee E2 
      ON E1.Salary>=E2.Salary 
      GROUP BY E2.Id,E2.Salary) A WHERE A.RANK=N
  );
END

你可能感兴趣的:(数据库学习)