【MySql】第二高薪水 第N高薪水 部门工资前三高的所有员工

    技术2025-04-05  19

    176.第二高薪水

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

    +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。 +---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+

    方法一

    //salary可能重复 所以需要使用distinct select distinct max(salary) as SecondHighestSalary from employee where salary < (select max(salary) from employee);

    方法二 Ifnull

    #ifnull(x,y),若x不为空则返回x,否则返回y,这道题y=null #limit x,y,找到对应的记录就停止 limit 的用法为: select * from tableName limit i,n tableName:表名 i:为查询结果的索引值(默认从0开始),当i=0时可省略i n:为查询结果返回的数量 i与n之间使用英文逗号","隔开 select IFNULL((select distinct(Salary) from Employee order by Salary desc limit 1,1),null) as SecondHighestSalary

    select * from table limit 2,1;

    //含义是跳过2条取出1条数据,limit后面是从第2条开始读,读取1条信息,即读取第3条数据

    ② select * from table limit 2 offset 1;

    //含义是从第1条(不包括)数据开始取出2条数据,limit后面跟的是2条数据,offset后面是从第1条开始读取,即读取第2,3条

    对MySQL来说, limit x,y = limit y offset x

    177.第N高薪水

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

    +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。 +------------------------+ | getNthHighestSalary(2) | +------------------------+ | 200 | +------------------------+ 那么要先查出前N薪水,然后取最小就好了, 注意可能总数不够前N,count一下比较即可 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN RETURN ( SELECT IF(count<N,NULL,min) FROM (SELECT MIN(Salary) AS min, COUNT(1) AS count FROM (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N) AS a ) as b ); END #还是上一题的思路,加上判断N<0时返回最小值 注意set的用法 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN if N<0 then RETURN (select min(Salary) from Employee); else set N = N-1; RETURN ( # Write your MySQL query statement below. select ifnull((select distinct Salary from Employee order by Salary desc limit N,1),null) as NthHighestSalay ); end if; END

    185. 部门工资前三高的所有员工

    Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。

    +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | | 7 | Will | 70000 | 1 | +----+-------+--------+--------------+ Department 表包含公司所有部门的信息。 +----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+ 编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回: +------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+

    解释:

    IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/department-top-three-salaries 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路:根据要输出表格的形式对select的列进行命名, select * from(窗口函数rank,dense_rank,rank_row over(partition 分类的列名 order by排序列名)as ranking from 表)as 表2 where加筛选条件 ranking<=3 and e.DepartmentId = d.Id #Write your MySQL query statement below Select d.name as 'Department',e.name as 'Emoloyee',e.Salary from (Select *,dense_rank() over ( partition by DepartmentId order by Salary Desc )as ranking from employee) as e,Department as d where ranking <=3 and e.DepartmentId = d.Id;
    Processed: 0.011, SQL: 9