LeetCode:Employees Earning More Than Their Manager

吃可爱长大的小学妹 提交于 2019-12-07 02:23:58

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!