1、题目名称
Employees Earning More Than Their Managers(比领导工资还高的员工)
2、题目地址
https://leetcode.com/problems/employees-earning-more-than-their-managers/
3、题目内容
Employee表的结构包含四列:Id、Name、Salary、ManagerId,找出其中工资高于直接上级的员工的Name。
例如,下表中Joe的工资大于他的直接上级Sam:
+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+
4、初始化数据库脚本
在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本:
-- 执行脚本前必须建立名为LEETCODE的DATABASE USE LEETCODE; DROP TABLE IF EXISTS Employee; CREATE TABLE Employee ( Id INT NOT NULL PRIMARY KEY, Name VARCHAR(50), Salary INT, ManagerId INT ); INSERT INTO Employee (Id, Name, Salary, ManagerId) VALUES (1, ‘Joe‘, 70000, 3); INSERT INTO Employee (Id, Name, Salary, ManagerId) VALUES (2, ‘Henry‘, 80000, 4); INSERT INTO Employee (Id, Name, Salary, ManagerId) VALUES (3, ‘Sam‘, 60000, NULL); INSERT INTO Employee (Id, Name, Salary, ManagerId) VALUES (4, ‘Max‘, 90000, NULL);
5、解题SQL
解题SQL如下:
SELECT Name FROM Employee AS A WHERE Salary > ( SELECT Salary FROM Employee AS B WHERE Id = A.ManagerId );
END
时间: 2024-11-12 09:40:47