算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。今天和大家聊的问题叫做 第N高的薪水  ,我们先来看题面:https://leetcode-cn.com/problems/nth-highest-salary/

Write a SQL query to get the nth highest salary from the Employee table.

题意

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

 

​LeetCode刷题实战177:第N高的薪水_SQL 查询

解题

思路:去重、排序、limit ,代码如下,已经在leetcode验证过了!

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 1 offset n
  );
END

好了,今天的文章就到这里