题目
Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
分析
1.查看表格
SELECT
*
FROM
employee;
2.连接两个表
SELECT
*
FROM
employee AS l1,
employee AS l2;
3.找到比自己经理薪水多的行
SELECT
*
FROM
employee AS l1,
employee AS l2
WHERE
l1.ManagerId = l2.Id
AND l1.Salary > l2.Salary;
4.按照题目要求,显示名字和行名
SELECT
l1.`Name` as Employee
FROM
employee AS l1,
employee AS l2
WHERE
l1.ManagerId = l2.Id
AND l1.Salary > l2.Salary;
解答
SELECT
l1.`Name` as Employee
FROM
employee AS l1,
employee AS l2
WHERE
l1.ManagerId = l2.Id
AND l1.Salary > l2.Salary;