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 | +----------+
WHERE
clause [Accepted]Algorithm
\nAs this table has the employee\'s manager information, we probably need to select information from it twice.
\nSELECT *\nFROM Employee AS a, Employee AS b\n;\n
\n\nNote: The keyword \'AS\' is optional.
\n
Id | \nName | \nSalary | \nManagerId | \nId | \nName | \nSalary | \nManagerId | \n
---|---|---|---|---|---|---|---|
1 | \nJoe | \n70000 | \n3 | \n1 | \nJoe | \n70000 | \n3 | \n
2 | \nHenry | \n80000 | \n4 | \n1 | \nJoe | \n70000 | \n3 | \n
3 | \nSam | \n60000 | \n\n | 1 | \nJoe | \n70000 | \n3 | \n
4 | \nMax | \n90000 | \n\n | 1 | \nJoe | \n70000 | \n3 | \n
1 | \nJoe | \n70000 | \n3 | \n2 | \nHenry | \n80000 | \n4 | \n
2 | \nHenry | \n80000 | \n4 | \n2 | \nHenry | \n80000 | \n4 | \n
3 | \nSam | \n60000 | \n\n | 2 | \nHenry | \n80000 | \n4 | \n
4 | \nMax | \n90000 | \n\n | 2 | \nHenry | \n80000 | \n4 | \n
1 | \nJoe | \n70000 | \n3 | \n3 | \nSam | \n60000 | \n\n |
2 | \nHenry | \n80000 | \n4 | \n3 | \nSam | \n60000 | \n\n |
3 | \nSam | \n60000 | \n\n | 3 | \nSam | \n60000 | \n\n |
4 | \nMax | \n90000 | \n\n | 3 | \nSam | \n60000 | \n\n |
1 | \nJoe | \n70000 | \n3 | \n4 | \nMax | \n90000 | \n\n |
2 | \nHenry | \n80000 | \n4 | \n4 | \nMax | \n90000 | \n\n |
3 | \nSam | \n60000 | \n\n | 4 | \nMax | \n90000 | \n\n |
4 | \nMax | \n90000 | \n\n | 4 | \nMax | \n90000 | \n\n |
> The first 3 columns are from a and the last 3 ones are from b. | \n\n | \n | \n | \n | \n | \n | \n |
Select from two tables will get the Cartesian product of these two tables. In this case, the output will be 4*4 = 16 records. However, what we interest is the employee\'s salary higher than his/her manager. So we should add two conditions in a WHERE
clause like below.
SELECT\n *\nFROM\n Employee AS a,\n Employee AS b\nWHERE\n a.ManagerId = b.Id\n AND a.Salary > b.Salary\n;\n
Id | \nName | \nSalary | \nManagerId | \nId | \nName | \nSalary | \nManagerId | \n
---|---|---|---|---|---|---|---|
1 | \nJoe | \n70000 | \n3 | \n3 | \nSam | \n60000 | \n\n |
As we only need to output the employee\'s name, so we modify the above code a little to get a solution.
\nMySQL
\nSELECT\n a.Name AS \'Employee\'\nFROM\n Employee AS a,\n Employee AS b\nWHERE\n a.ManagerId = b.Id\n AND a.Salary > b.Salary\n;\n
JOIN
clause [Accepted]Algorithm
\nActually, JOIN
is a more common and efficient way to link tables together, and we can use ON
to specify some conditions.
SELECT\n a.NAME AS Employee\nFROM Employee AS a JOIN Employee AS b\n ON a.ManagerId = b.Id\n AND a.Salary > b.Salary\n;\n