LeetCode:181.超过经理收入的员工
题目链接: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.超过经理收入的员工的更多相关文章
- [LeetCode] 181.超过经理收入的员工
Employee表包含所有员工,他们的经理也属于员工.每个员工都有一个 Id,此外还有一列对应员工的经理的 Id. +----+-------+--------+-----------+ | Id | ...
- 181. 超过经理收入的员工 + join + MySql
181. 超过经理收入的员工 LeetCode_MySql_181 题目描述 方法一:笛卡尔积 # Write your MySQL query statement below select e1.N ...
- sql 181. 超过经理收入的员工
Employee 表包含所有员工,他们的经理也属于员工.每个员工都有一个 Id,此外还有一列对应员工的经理的 Id. +----+-------+--------+-----------+| Id | ...
- LeetCode 181. Employees Earning More Than Their Managers (超过经理收入的员工)
题目标签: 题目给了我们一个 员工表,包括经理.员工会有经理的id. 这里可以重复 利用两次 表格,表格a, 表格b,当a 员工的经理id 等于 b员工时候,在从中找到员工工资大于经理的.具体看co ...
- [SQL]LeetCode181. 超过经理收入的员工 | Employees Earning More Than Their Managers
SQL架构 Create table If Not Exists Employee (Id ), Salary int, ManagerId int) Truncate table Employee ...
- 超过经理收入的员工 表的自JOIN
https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/description/ The Employe ...
- leetcode 184 部门工资最高的员工
题目描述:Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. Department 表包含公司所有部门的信息. 编写一个 SQL 查询,找 ...
- leetcode 181 Employees Earning More Than Their Managers 不会分析的数据库复杂度
https://leetcode.com/problems/employees-earning-more-than-their-managers/description/ 老师上课没分析这些的复杂度, ...
- Leetcode 181. Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
随机推荐
- Bootstrap4从入门到精通视频教程
一.布局 0.课件1.Bootstrap介绍_栅格系统2.禁用响应式_响应式分界点 二.内容 3.排版_代码4.图片_图片框5.表格 三.公共样式 6.边框_浮动7.颜色_Display显示属性8.文 ...
- P1115 最大子段和&P1719 最大加权矩形
上接:DP&图论 DAY 1 上午 这两个题本质是一个亚子,所以放一起啦 DPDPDPDPDPDPDPDP P1115 最大子段和 题解 因为题目要求的是一段连续的区间,所以前缀和搞暴力??? ...
- centos7 安装 ftp 服务及创建 repo源
安装 ftp 服务 安装和启动服务:# yum install vsftpd# systemctl enable vsftpd# systemctl start vsftpd 配置文件: vi /et ...
- CentOS / RHEL 配置yum源
CentOS / RHEL 配置yum源 */--> CentOS / RHEL 配置yum源 Table of Contents 1. 前言 2. 关于yum 2.1. yum是什么 2.2. ...
- cefsharp wpf
github 安装 PM> Install-Package CefSharp.Wpf 解决方案->属性->配置属性->活动解决方案平台-新建-x64 在需要使用的窗体上引用xm ...
- Python3+RobotFramewok 快速入门(二)
1. 原理 首先解释一下RF的工作原理,官方文档介绍就不赘述了,笔者就框架架构做出一个更加具体的描述 测试套及测试用例集(Test Data即需要用户编写的脚本)通过RF特定的语法解析,然后知道用户要 ...
- Java中非静态成员变量、静态成员变量的初始化时机
转: Java中非静态成员变量.静态成员变量的初始化时机. 2018年05月22日 11:48:11 SilenceCarrot 阅读数 421 版权声明:技术就要分享才有意思,欢迎大家分享(注明 ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-4.微信授权一键登录开发之授权URL获取
笔记 4.微信授权一键登录开发之授权URL获取 简介:获取微信开放平台扫码连url地址 1.增加结果工具类,JsonData; 增加application.properties配置 ...
- python-Web-数据库-mysql
概念: 服务器->数据库管理系统(软件)->数据库(文件夹)->表(文件) 关系型 安装与配置: >>>下载-安装-环境变量 >>>启动 mysq ...
- STS中依赖项的设置
经过试验,把依赖项总结一下,可能会不断修改. 1. 父依赖项(固定) <parent> <groupId>org.springframework.boot</groupI ...