IT编程 > 数据库 > Mysql

LeetCode——Employees Earning More Than Their Managers

29人参与2019-10-09

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

这种单表比较条件,一般都是表内进行join操作.
参照此思路,解题如下所示:

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

运行效率在可以接受的范围,此外语句也较为清晰便于维护.

您希望与广大热心网友互动!!点此进行留言回复

相关推荐

LeetCode——Employees Earning More Than Their Managers

10-09

浅谈mysql的索引设计原则以及常见索引的区别

06-04

InnoDB On-Disk Structures(四)--Doublewrite Buffer (转载)

08-21

mysql获取本周五到上周四的数据SUBDATE()DATE_FORMAT()的方法

07-04

ubuntu16.04下安装petalinux 2017.4的方法

01-02

MySQL命令行导出与导入数据库

06-07

MySQL数据库优化经验详谈(服务器普通配置)第1/3页

06-05

在linux下,把程序设置systemctl服务,并开机启动

07-04

MySQL编译安装及启动

07-15

MySQL存储过程中一些基本的异常处理教程

06-08

最近更新

Linux搭建MySQL主从

10-20

MySQL实战45讲

10-20

“GIS DICTIONARY A-Z” 查询页面开发(2)——INSERT INTO数据库

10-20

MySQL如何进行索引重建操作?

10-20

MySQL truncate含有外键约束的条目报错

10-20

MySQL复制从库建立-xtracebackup方式

10-19

Python学习日记(四十) Mysql数据库篇 八

10-19

mysql查询表大小

10-19

MySQL修炼之路一

10-19

MySQL修炼之路二

10-19

网友评论

已有0条评论