-
Notifications
You must be signed in to change notification settings - Fork 0
/
181.employees-earning-more-than-their-managers.2.sql
42 lines (42 loc) · 1.33 KB
/
181.employees-earning-more-than-their-managers.2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
--
-- @lc app=leetcode id=181 lang=mysql
--
-- [181] Employees Earning More Than Their Managers
--
-- https://leetcode.com/problems/employees-earning-more-than-their-managers/description/
--
-- database
-- Easy (47.14%)
-- Total Accepted: 103.7K
-- Total Submissions: 218.3K
-- Testcase Example: '{"headers": {"Employee": ["Id", "Name", "Salary", "ManagerId"]}, "rows": {"Employee": [[1, "Joe", 70000, 3], [2, "Henry", 80000, 4], [3, "Sam", 60000, null], [4, "Max", 90000, null]]}}'
--
-- 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 |
-- +----------+
--
--
--
SELECT e1.Name as Employee FROM Employee e1, Employee e2
WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary