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 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+----------+
| Employee |
+----------+
| Joe |
+----------+
思路一:用exists
 SELECT Name from Employee e1 where EXISTS (SELECT ID FROM Employee e2 where e1.ManagerId = e2.Id AND e1.Salary > e2.Salary);

自连接

SELECT e1.Name FROM Employee e1 LEFT JOIN Employee e2 ON e1.ManagerId = e2.Id WHERE e1.Salary > e2.Salary;

Employees Earning More Than Their Managers的更多相关文章

  1. [LeetCode] Employees Earning More Than Their Managers 员工挣得比经理多

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

  2. LeeCode(Database)-Employees Earning More Than Their Managers

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

  3. Leetcode 181. Employees Earning More Than Their Managers

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

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

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

  5. 【SQL】181. Employees Earning More Than Their Managers

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

  6. LeetCode - Employees Earning More Than Their Managers

    Description: The Employee table holds all employees including their managers. Every employee has an ...

  7. 181. Employees Earning More Than Their Managers

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

  8. DataBase -- Employees Earning More Than Their Managers My Submissions Question

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

  9. LeetCode——Employees Earning More Than Their Managers

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

随机推荐

  1. /* font-awesome-4.7.0的应用*/

    <!DOCTYPE html> /* font-awesome-4.7.0的应用*/ <html lang="en"> <head> <m ...

  2. linux压缩包安装jdk

    1.下载jdk压缩包 $ wget http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa00 ...

  3. mongodb 修改操作

    $addToSet与$each结合完成批量数组更新 db.text.update({_id:1000},{$addToSet:{books:{$each:["js","d ...

  4. 1.1 xss原理分析与剖析(2)

    0×01 反射型XSS: 反射XSS是XSS分类中最多的,他们原理是下面这样: Hacker——发现存在反射XSS的URL——根据输出点的环境构造XSS代码——进行编码.缩短(可有可无,是为了增加迷惑 ...

  5. 用ORBSLAM2运行TUM Dataset数据集Monocular Examples

    参照https://github.com/raulmur/ORB_SLAM2/blob/master/README.md 运行 4. Monocular Examples TUM Dataset 数据 ...

  6. Linux常用知识点汇总

    常用命令 1.ls 列出目录下的所有文件及文件夹 2.pwd 打印出当前所在目录 3. ./ 执行 .sh 文件命令 4.ip addr 查看ip地址 5.sudo  service network ...

  7. Django 之装饰器实现登录认证

    def check_login(func): # 自定义登录验证装饰器 def warpper(request, *args, **kwargs): is_login = request.sessio ...

  8. 关于.net中的身份认证(AuthorizeAttribute)的问题

    引言 新公司当初面试的时候问了我很多用户验证的问题,这里就把我的对于验证的想法写一下,希望可以有一个系统的学习记录. 一.如何验证 B/S结构的请求是http请求,个人理解的http请求有两个特点:1 ...

  9. js对象转换为json字符串

    JavaScript的对象是一组由键-值组成的无序集合,例如: var person = { name: 'Bob', age: 20, tags: ['js', 'web', 'mobile'], ...

  10. Java中Class Type 类类型是怎么回事?

    Java中三种方式可以用来表示Class Type(类的实例对象), 第一种,通过隐藏的静态成员变量class来表示:第二种,通过调用该类的对象的getClass方法:第三种,通过Class.forN ...