题目链接: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

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

解答

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

  1. ---- oracle ----
  2. /* Write your PL/SQL query statement below */
  3. select c.Name as Employee
  4. from
  5. (
  6. select a.Name,
  7. (a.Salary - b.Salary) as Salary
  8. from Employee a
  9. left join Employee b
  10. on a.ManagerID = b.Id
  11. ) c
  12. where c.Salary > 0 ---- 868ms

应该可以继续优化的。。

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

通过where实现

  1. ---- MySQL ----
  2. select a.Name as Employee
  3. from Employee a,
  4. Employee b
  5. where a.ManagerId = b.Id
  6. 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. MySQL5.7 创建及查看数据库

    1.创建数据库语句create database语句是在MySQL实例上创建一个指定名称的数据库.create schema语句的语义和create database是一样的. 2.语法解析 CREA ...

  2. Docker-----关于dockerfile

    docker build参数说明 --no-cache :创建镜像的过程不使用缓存: --force-rm :设置镜像过程中删除中间容器: --network=host:容器会使用宿主机的网络,容器与 ...

  3. jeecg随笔

    1.根据数据字典code查找该字典下的元素: SELECT typecode,typename from t_s_type where typegroupid=(select id from t_s_ ...

  4. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-2.微信扫一扫功能开发前期准备

    笔记 2.微信扫一扫功能开发前期准备         简介:讲解微信扫一扫功能相关开发流程和资料准备              1.微信开放平台介绍(申请里面的网站应用需要企业资料)          ...

  5. 一百:CMS系统之修改密码逻辑

    定义一个基类form,用于获取错误信息 from wtforms import Form class BaseForm(Form): def get_error(self): # a = {'aaa' ...

  6. C#创建windows服务(一:初识windows服务)

    一 . 服务简介 Microsoft Windows 服务(过去称为 NT 服务)允许用户创建可在其自身的 Windows 会话中长时间运行的可执行应用程序. 这些服务可在计算机启动时自动启动,可以暂 ...

  7. jenkins常用插件安装

    1.常用jenkins插件 插件相关下载地址:http://updates.jenkins-ci.org/download/plugins/ git.hpi git-client.hpi gitlab ...

  8. Git(2):基本操作

    Git 创建仓库 执行<git init>命令后,Git仓库会生成一个.git目录,该目录包含了资源的所有元数据,其他的项目目录保持不变(Git 只在仓库的根目录生成 .git 目录). ...

  9. 二、windows下搭建vue开发环境+IIS部署

    有时我们的服务器并不一定是node,也许是IIS,这样我们就需要把工程构建出来,与IIS集成. 构建该项目的命令如下 cnpm run build 将dist文件夹拷贝出来,放到IIS的发布目录,在浏 ...

  10. IntelliJ IDEA入门之常用配置以及问题解决(持续更新中)

    软件版本: IntelliJ IDEA 2019.1.1(Ultimate Edition) 运行环境: JDK1.8, Tomcat8.0, Maven3.6 我们在学习新的无论是jar包, 框架, ...