题目链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/

题目

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

+----+-------+--------+-----------+

| Id | Name | Salary | ManagerId |

+----+-------+--------+-----------+

| 1 | Joe | 70000 | 3 |

| 2 | Henry | 80000 | 4 |

| 3 | Sam | 60000 | NULL |

| 4 | Max | 90000 | NULL |

+----+-------+--------+-----------+

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

+----------+

| Employee |

+----------+

| Joe |

+----------+

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解答

第一想法,通过自连接,再做减法。

---- oracle ----
/* Write your PL/SQL query statement below */
select c.Name as Employee
from
(
select a.Name,
(a.Salary - b.Salary) as Salary
from Employee a
left join Employee b
on a.ManagerID = b.Id
) c
where c.Salary > 0 ---- 868ms

应该可以继续优化的。。

---- oracle ----
/* Write your PL/SQL query statement below */
select a.Name as Employee
from Employee a
left join Employee b
on a.ManagerID = b.Id
where a.Salary > b.Salary ---- 528ms

通过where实现

---- MySQL ----
select a.Name as Employee
from Employee a,
Employee b
where a.ManagerId = b.Id
and a.Salary > b.Salary; ---- 260ms

思考

内连接会自动过滤null,所以关联的时候无须再设定ManagerId is not null过滤条件。

子查询性能 & 关联查询,到底哪个快?验证一番。

直接通过from table a, table b会产生笛卡尔积,影响效率。

另外,还可通过子查询和exists进行解答,不过效率都不如连接来得快,就不进行测试了。

LeetCode:181.超过经理收入的员工的更多相关文章

  1. [LeetCode] 181.超过经理收入的员工

    Employee表包含所有员工,他们的经理也属于员工.每个员工都有一个 Id,此外还有一列对应员工的经理的 Id. +----+-------+--------+-----------+ | Id | ...

  2. 181. 超过经理收入的员工 + join + MySql

    181. 超过经理收入的员工 LeetCode_MySql_181 题目描述 方法一:笛卡尔积 # Write your MySQL query statement below select e1.N ...

  3. sql 181. 超过经理收入的员工

    Employee 表包含所有员工,他们的经理也属于员工.每个员工都有一个 Id,此外还有一列对应员工的经理的 Id. +----+-------+--------+-----------+| Id | ...

  4. LeetCode 181. Employees Earning More Than Their Managers (超过经理收入的员工)

    题目标签: 题目给了我们一个 员工表,包括经理.员工会有经理的id. 这里可以重复 利用两次 表格,表格a, 表格b,当a 员工的经理id  等于 b员工时候,在从中找到员工工资大于经理的.具体看co ...

  5. [SQL]LeetCode181. 超过经理收入的员工 | Employees Earning More Than Their Managers

    SQL架构 Create table If Not Exists Employee (Id ), Salary int, ManagerId int) Truncate table Employee ...

  6. 超过经理收入的员工 表的自JOIN

    https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/description/ The Employe ...

  7. leetcode 184 部门工资最高的员工

    题目描述:Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. Department 表包含公司所有部门的信息. 编写一个 SQL 查询,找 ...

  8. leetcode 181 Employees Earning More Than Their Managers 不会分析的数据库复杂度

    https://leetcode.com/problems/employees-earning-more-than-their-managers/description/ 老师上课没分析这些的复杂度, ...

  9. Leetcode 181. Employees Earning More Than Their Managers

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

随机推荐

  1. JAVA 基础编程练习题13 【程序 13 根据条件求数字】

    13 [程序 13 根据条件求数字] 题目:一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少? 程序分析:在 10 万以内判断,先将该数加上 100 后 ...

  2. Spring-Kafka —— KafkaListener禁止自启动

    应用服务启动时,KafkaListener默认会自动启动进行消费,如果想不自动消费,可以设置AutoStartup属性值为false @Override @KafkaListener(id = Con ...

  3. 【笔记】《CNCF × Alibaba云原生技术公开课》知识点

    一,课时1:第一堂“云原生”课 二,课时2:容器基本概念 1.已运行 docker run -d -t —name demo ubuntu top 命令, 是否可以在 demo 这个容器内部停止容器? ...

  4. 转:微服务框架之微软Service Fabric

    常见的微服务架构用到的软件&组件: docker(成熟应用) spring boot % spring cloud(技术趋势) Service Fabric(属于后起之秀 背后是微软云的驱动) ...

  5. docker笔记(2)——docker镜像操作

    操作环境:mac OS 10.14.6 docker版本:10.03.1 终端:iterm2 3.3 时间:2019年8月 docker 镜像,是运行容器的模板,通过pull操作会向指定仓库获取镜像, ...

  6. ReDOS攻击

    正则表达式拒绝服务攻击(Regular Expression Denial of Service)当开发人员编写的正则表达式存在缺陷时,攻击者可以构造特殊的字符串来大量消耗服务器资源,最终造成拒绝服务 ...

  7. AspxGridView 弹框选择器 JS

    function Dictionary() { this.data = new Array(); this.put = function (key, value) { this.data[key] = ...

  8. 论文阅读 | Robust Neural Machine Translation with Doubly Adversarial Inputs

    (1)用对抗性的源实例攻击翻译模型; (2)使用对抗性目标输入来保护翻译模型,提高其对对抗性源输入的鲁棒性. 生成对抗输入:基于梯度 (平均损失)  ->  AdvGen 我们的工作处理由白盒N ...

  9. 10.hive安装

    上传hive安装包并解压 给hive设置一个软链接 给hive配置环境变量 sudo vim /etc/profile #hive export HIVE_HOME=/opt/modules/hive ...

  10. ajax同源策略,jsonP跨域访问

    浏览器处于安全性的考虑,要求ajax请求,必须满足同源策略 规定:访问的协议://域名:端口号都相同时满足同源策略,浏览器可以正确解析数据,否则如果有一项不满足要求,则属于跨域访问,浏览器可以正常获取 ...