Employee
The 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 Employee
table, 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 JOIN
two tables (the same table), picking up the on
specified 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