【题目】
Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名Name。
Employee表头 Id | Name | Salary | ManagerId
在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
来源:leetcode
链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/
【代码】
【join大法好】效率非常优秀了
执行用时 :257 ms, 在所有 MySQL 提交中击败了95.19% 的用户
内存消耗 :0B, 在所有 MySQL 提交中击败了100.00%的用户
# Write your MySQL query statement below
select a.Name as Employee from Employee as a join Employee as bon a.ManagerId=b.Id and a.Salary>b.Salary
【一个select】
执行用时 :280 ms, 在所有 MySQL 提交中击败了63.02% 的用户
内存消耗 :0B, 在所有 MySQL 提交中击败了100.00%的用户
# Write your MySQL query statement below
select a.Name as Employee from Employee a,Employee b
where a.ManagerId=b.Id and a.Salary>b.Salary
【两个select】这个很慢
# Write your MySQL query statement below
select Name as Employee from Employee a where ManagerId and Salary >
(select Salary from Employee b where a.ManagerId=b.Id)

本文介绍了一道来自LeetCode的SQL挑战题,要求找出薪资超过其经理的员工。通过三种不同的SQL查询方法,包括使用JOIN语句、子查询和多表连接,展示了如何高效地解决这个问题。
332

被折叠的 条评论
为什么被折叠?



