,敢教日月换新天。为有牺牲多壮志

[SQL]LeetCode181. 超过经理收入的员工 | Employees Earning More Than Their Managers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10152507.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
1 Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int)
2 Truncate table Employee
3 insert into Employee (Id, Name, Salary, ManagerId) values ('1', 'Joe', '70000', '3')
4 insert into Employee (Id, Name, Salary, ManagerId) values ('2', 'Henry', '80000', '4')
5 insert into Employee (Id, Name, Salary, ManagerId) values ('3', 'Sam', '60000', 'None')
6 insert into Employee (Id, Name, Salary, ManagerId) values ('4', 'Max', '90000', 'None')

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

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

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

182ms
复制代码
 1 # Write your MySQL query statement below
 2 select E1.Name as Employee 
 3         from(
 4             select * from Employee
 5         )E1
 6         left join 
 7         (
 8             select * from Employee
 9                 group by id
10         )E2
11         on E1.ManagerId = E2.Id
12         where E1.Salary > E2.Salary
复制代码

185ms

1 SELECT b.Name AS Employee
2 FROM Employee a
3 JOIN Employee b
4 ON a.ID = b.ManagerID
5 WHERE b.Salary > a.Salary;

647ms

1 /* Write your T-SQL query statement below */
2 SELECT a.Name AS Employee
3 FROM Employee a
4     JOIN Employee b ON a.ManagerId = b.Id
5 WHERE a.Salary > b.Salary

 

posted @   为敢技术  阅读(325)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°