欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL实现LeetCode(181.员工挣得比经理多)

程序员文章站 2022-03-10 10:28:42
[leetcode] 181.employees earning more than their managers 员工挣得比经理多the employee table holds...

[leetcode] 181.employees earning more than their managers 员工挣得比经理多

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

这道题给我们了一个employee表,里面有员工的薪水信息和其经理的信息,经理也属于员工,其经理id为空,让我们找出薪水比其经理高的员工,那么就是一个很简单的比较问题了,我们可以生成两个实例对象进行内交通过managerid和id,然后限制条件是一个salary大于另一个即可:

解法一:

select e1.name from employee e1
join employee e2 on e1.managerid = e2.id
where e1.salary > e2.salary;

我们也可以不用join,直接把条件都写到where里也行:

解法二:

select e1.name from employee e1, employee e2
where e1.managerid = e2.id and e1.salary > e2.salary;

参考资料:

到此这篇关于sql实现leetcode(181.员工挣得比经理多)的文章就介绍到这了,更多相关sql实现员工挣得比经理多内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!