leetcode 181. Employees Earning More Than Their Managers

题目链接:

https://leetcode.com/problems/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      |
+----------+

Subscribe to see which companies asked this question

1 # Write your MySQL query statement below
2 select e1.Name from
3     Employee e1,Employee e2
4         where e1.Salary>e2.Salary
5             and e1.ManagerId=e2.Id

http://www.w3school.com.cn/sql/sql_join.asp

关于SQL Jion

SQL JOIN

SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据。

Join 和 Key

有时为了得到完整的结果,我们需要从两个或更多的表中获取结果。我们就需要执行 join。

数据库中的表可通过键将彼此联系起来。主键(Primary Key)是一个列,在这个列中的每一行的值都是唯一的。在表中,每个主键的值都是唯一的。这样做的目的是在不重复每个表中的所有数据的情况下,把表间的数据交叉捆绑在一起。

请看 "Persons" 表:

Id_P LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

请注意,"Id_P" 列是 Persons 表中的的主键。这意味着没有两行能够拥有相同的 Id_P。即使两个人的姓名完全相同,Id_P 也可以区分他们。

接下来请看 "Orders" 表:

Id_O OrderNo Id_P
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 65

请注意,"Id_O" 列是 Orders 表中的的主键,同时,"Orders" 表中的 "Id_P" 列用于引用 "Persons" 表中的人,而无需使用他们的确切姓名。

请留意,"Id_P" 列把上面的两个表联系了起来。

引用两个表

我们可以通过引用两个表的方式,从两个表中获取数据:

谁订购了产品,并且他们订购了什么产品?

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P

结果集:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678

SQL JOIN - 使用 Join

除了上面的方法,我们也可以使用关键词 JOIN 来从两个表中获取数据。

如果我们希望列出所有人的定购,可以使用下面的 SELECT 语句:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName

结果集:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678

不同的 SQL JOIN

除了我们在上面的例子中使用的 INNER JOIN(内连接),我们还可以使用其他几种连接。

下面列出了您可以使用的 JOIN 类型,以及它们之间的差异。

  • JOIN: 如果表中有至少一个匹配,则返回行
  • LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
  • RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
  • FULL JOIN: 只要其中一个表中存在匹配,就返回行
时间: 2024-08-10 02:10:46

leetcode 181. Employees Earning More Than Their Managers的相关文章

【Leetcode】Employees Earning More Than Their Managers

题目链接:https://leetcode.com/problems/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

Database 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

LeetCode OJ 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 |

LeetCode SQL:Employees Earning More Than Their Managers

# Write your MySQL query statement below SELECT a.Name FROM Employee AS a INNER JOIN Employee AS b ON a.ManagerId = b.Id WHERE a.Salary > b.Salary 让我来看看讨论区,并没有发现更好的

181. Employees Earning More Than Their Managers

# Write your MySQL query statement below select E.name Employee from Employee as E,Employee as M where E.ManagerId=M.Id and E.Salary>M.Salary

LeetCode:Employees Earning More Than Their Manager

1.题目名称 Employees Earning More Than Their Managers(比领导工资还高的员工) 2.题目地址 https://leetcode.com/problems/employees-earning-more-than-their-managers/ 3.题目内容 Employee表的结构包含四列:Id.Name.Salary.ManagerId,找出其中工资高于直接上级的员工的Name. 例如,下表中Joe的工资大于他的直接上级Sam: +----+-----

[LeetCode] 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 |

【leetcode SQL】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 |

LeetCode - Employees Earning More Than Their Managers

Description: The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. Given the Employee table, write a SQL query that finds out employees who earn more than their manag