2022-11-21:第N高的薪水。表结构和数据的sql语句如下。请问sql语句如何写?

DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
  id int(11) NOT NULL,
  salary int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO employee VALUES ('1', '100');
INSERT INTO employee VALUES ('2', '200');
INSERT INTO employee VALUES ('3', '300');

需要写的函数如下:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      # 在这里写sql语句
      
  );
END

答案2022-11-21:

sql语句如下:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    SET N := N-1;
  RETURN (
      SELECT 
            salary
      FROM 
            employee
      GROUP BY 
            salary
      ORDER BY 
            salary DESC
      LIMIT N, 1
  );
END

select getNthHighestSalary(2);

执行结果如下: 在这里插入图片描述