力扣181(MySQL)- 超过经理收入的员工(简单)

题目:

表:Employee 

编写一个SQL查询来查找收入比经理高的员工。

以 任意顺序 返回结果表。

查询结果格式如下所示。

示例 1:

 解题思路:

一、【子查询】

先通过子查询找到当前员工的经理的工资,再将员工的工资与子查询中找到的经理的工资进行比较。

MySQL语句为:

1 # Write your MySQL query statement below
2 select name as Employee
3 from Employee a
4 where a.salary > (select salary from Employee b where a.managerId = b.id);

 

 二、【自连接】

将一张表变成两张表,第一张表作为主体,第二张表作为展示经理和工资的附表,用join...on...将两张表进行连接,这样每个员工后面也会展示出经理的工资

Employee a join Employee b on a.managerId = b.id的结果为:

MySQL语句为:

1 select a.name as Employee
2 from Employee a join Employee b on a.managerId = b.id
3 where a.salary > b.salary

 

posted on 2022-12-11 15:45  我不想一直当菜鸟  阅读(145)  评论(0编辑  收藏  举报