[LeetCode] more than the manager of employee income

EmployeeThe following table contains all employees, their managers belong to employees. Each employee has a Id, in addition to a correspondence Id manager of staff.

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

Given Employeetable, write a SQL query that you can get the names of managers earning more than their employees. In the above table, Joe is the only one earning more than his manager's staff.

+----------+
| Employee |
+----------+
| Joe      |
+----------+

answer:

Method a : , 查出两个表(都是Employee表)respectively, As a, b,

SELECT
    *
FROM
    Employee AS a,
    Employee AS b
WHERE
    a.ManagerId = b.Id
        AND a.Salary > b.Salary

Method two : using JOINtwo tables (the same table), picking up the onspecified condition (a.ManagerId = b.Id)

SELECT
     a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
     ON a.ManagerId = b.Id
     AND a.Salary > b.Salary

Excerpt: https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/solution/chao-guo-jing-li-shou-ru-de-yuan-gong-by-leetcode/

Guess you like

Origin www.cnblogs.com/qiuhuashan/p/11071986.html