题目链接: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. 【Webscraper】不懂编程也能爬虫

    一.配置环境 在浏览器中安装web scraper插件. 所有安装包下载链接: https://pan.baidu.com/s/1CfAWf0wMO6WqicoUgdYgkg 提取码: nn2e 安装 ...

  2. js 操作select和option常见用法

    1.获取选中select的value和text,html <select id="mySelect"> <option value="1"&g ...

  3. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-4.微信授权一键登录开发之授权URL获取

    笔记 4.微信授权一键登录开发之授权URL获取     简介:获取微信开放平台扫码连url地址 1.增加结果工具类,JsonData;  增加application.properties配置      ...

  4. cmake编译排除文件夹

    在CMakeLists.txt中,要对某些子文件夹全部进行编译则使用 add_subdirectory(examples) 那如果在examples文件夹中建立了某个目录如examples/venv, ...

  5. 2. bash基础

    通配符 通配符 功能说明 实例 * 匹配所有字符 ls *.o ? 匹配所有的当个字符 ls net??? [a-z] 匹配属于a到z范围集合内的一个字符 ls [a-i]* [...] 与方括号内的 ...

  6. How to remove duplicate lines in a large text file?

    How would you remove duplicate lines from a file that is  much too large to fit in memory? The dupli ...

  7. 在java中异常中的题目---重要的一点

    public classTest { public static void main(String[] args) { System.out.println(newTest().test()); } ...

  8. flask(3.0)

    目录 一.Flask - CBV 二.Flask - Session 1.安装flask-session 2.回顾flask自带的session的使用方法 3.flask-session的使用(以保存 ...

  9. maven 依赖原则

    maven 依赖原则 ###间接依赖路径最短优先 a->b->c1.0 a->e->f->c1.1 ====>c1.0 申明顺序优先 <!-- test1 - ...

  10. zabbix3.0升级到4.0

    升级步鄹: 3.0->3.2 1.停服务 service zabbix-server stop 2.备份配置文件 #cp /etc/zabbix/zabbix_server.conf /data ...