Question:
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 |+----------+
Analysis:
Employee表格包含公司里所有的员工,包括他们的经理。每个员工都有一个员工ID和一个经理ID。
给出Employee表格,写一个SQL语句,找出工资比他的经理还高的员工。例如在上面的例子中,只有Joe满足该情况。
最后给出什么则select后面接什么,如果遇到比较的,则应该选择使用两个表格。
Answer:
select e1.Name from Employee e1, Employee e2where e2.Id = e1.ManagerId and e1.Salary > e2.Salary