IT编程 > 数据库 > Mysql

LeetCode——Employees Earning More Than Their Managers

85人参与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创建表时报错error150

04-03

使用Rotate Master实现MySQL 多主复制的实现方法

06-03

通过一次服务器错误代码和消息了解mysql如何处理异常

03-12

探讨:sql插入空,默认1900-01-01 00:00:00.000的解决方法详解

07-04

MYSQL 游标学习及使用实例

12-07

在MySQL中使用序列的简单教程

06-05

linux系统下jdk的安装配置教程

07-04

windows下mysql 5.7版本中修改编码为utf-8的方法步骤

12-10

mysql表的基础操作汇总(三)

07-04

最近更新

B树、B-树、B+树、B*树都是什么

05-12

mysql语句执行顺序是怎么样的,导致查询语句不同结果

05-12

MySQL——创建数据库与表

05-12

MySQL表的创建、修改、删除,表的字段类型有哪些

05-12

数据库mysql主从复制的原理和如何搭建主从复制

05-12

数据库mysql高可用配置架构部署实施

05-12

mysql数据库读写分离 Atlas架构

05-12

MySQL in CentOS 7 安装部署

05-12

Mysql用户管理相关

05-12

mysql 存储过程中双重循环嵌套的应用

05-12

网友评论