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

作者:Grandyang 时间:2024-01-17 03:15:01 

[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;

参考资料:

https://leetcode.com/discuss/88189/two-straightforward-way-using-where-and-join

来源:https://www.cnblogs.com/grandyang/p/5354306.html

标签:SQL,员工挣得比经理多,LeetCode
0
投稿

猜你喜欢

  • Python树莓派学习笔记之UDP传输视频帧操作详解

    2023-04-21 10:03:20
  • tensorflow模型转ncnn的操作方式

    2022-10-29 15:55:19
  • pandas实现按行选择的示例代码

    2021-08-04 01:53:50
  • python调用文件时找不到相对路径的解决方案

    2021-08-21 14:38:24
  • Python实现图片格式转换

    2023-08-03 04:58:57
  • Python pickle模块实现对象序列化

    2022-03-04 03:51:22
  • Python中三种时间格式转换的方法

    2023-10-18 13:11:43
  • Python xlrd读取excel日期类型的2种方法

    2021-06-28 02:12:53
  • python Selenium 库的使用技巧

    2021-08-07 21:23:47
  • 详解如何在阿里云上安装mysql

    2024-01-14 08:25:57
  • Python 虚拟环境工作原理解析

    2023-02-21 02:18:50
  • MySQL数据库Shell import_table数据导入

    2024-01-15 02:34:55
  • 使用Python设置tmpfs来加速项目的教程

    2023-12-24 09:38:34
  • Python数据分析之彩票的历史数据

    2021-06-16 07:53:39
  • python数据化运营的重要意义

    2021-05-05 21:02:27
  • MySQL使用表锁和行锁的场景详解

    2024-01-24 05:59:39
  • 分享8点超级有用的Python编程建议(推荐)

    2022-03-31 08:05:57
  • python 获取字典特定值对应的键的实现

    2022-07-01 19:25:21
  • Python答题卡识别并给出分数的实现代码

    2022-04-10 03:52:46
  • vue iview的菜单组件Mune 点击不高亮的解决方案

    2024-06-07 15:20:08
  • asp之家 网络编程 m.aspxhome.com