LeetCode:Employees Earning More Than Their Manager

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

LeetCode:Employees Earning More Than Their Manager的相关文章

leetcode 181. Employees Earning More Than Their Managers

题目链接: https://leetcode.com/problems/employees-earning-more-than-their-managers/ The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. +----+-------+--------+---------

【Leetcode】Employees Earning More Than Their Managers

题目链接:https://leetcode.com/problems/employees-earning-more-than-their-managers/ 题目: The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. +--+---+--–+---–+ | Id | Name

LeetCode OJ Employees Earning More Than Their Managers

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. +----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 |

LeetCode SQL:Employees Earning More Than Their Managers

# Write your MySQL query statement below SELECT a.Name FROM Employee AS a INNER JOIN Employee AS b ON a.ManagerId = b.Id WHERE a.Salary > b.Salary 让我来看看讨论区,并没有发现更好的

LeetCode:Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

leetcode:Pascal's Triangle

一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 须要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方式. 每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1). 算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数

LeetCode: Triangle 题解

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s