leetcode MYSQL数据库题目181

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_30650153/article/details/82216078

181.Employees Earning More Than Their Managers

1、题目与答案

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

Employee中包含包括管理者在内的所有员工,每一个员工都有一个id,还有一列表示该员工的管理者的 id。

Table:Employees:

+----+-------+--------+-----------+
| 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.

写出SQL语句,找出收入比其管理者还多的员工,在上面的那张表中,Joe就是唯一符合条件的。

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

答案:
select distinct em1.name as Employee
from employee em1,employee em2
where em1.managerId=em2.id and em1.salary > em2.salary;


2、知识点总结

验证步骤:

create table employee(
id int primary key not NULL,
name varchar(50) ,
salary int ,
managerId int
);
insert into employee()
values
(1,"Joe",70000,3),
(2,"Henry",80000,4),
(3,"Sam",60000,NULL),
(4,"Max",90000,NULL);

知识点:

  • select ... as作为列名
  • where条件筛选

猜你喜欢

转载自blog.csdn.net/qq_30650153/article/details/82216078