Reputation: 55
There is a table Employee
as below:
I need finds out employees who earn more than their managers and I figure out two methods:
SELECT a.Name AS Employee FROM Employee a, Employee b WHERE a.Salary > b.Salary AND a.ManagerId = b.Id;
SELECT a.Name AS Employee FROM Employee a INNER JOIN Employee b ON a.Salary > b.Salary AND a.ManagerId = b.Id;
Both of them work well and have close speed.
What's the difference between them? Thanks.
Upvotes: 0
Views: 33
Reputation: 62831
Those queries are equivalent. But you should use the join
syntax instead of commas in the from
clause. INNER JOIN ON vs WHERE clause
Here's an alternative option which might have a better performance using exists
:
select e.Name AS Employee
from employee e
where exists (
select 1
from employee e2
where e.managerid = e2.id and e.salary > e2.salary
)
Upvotes: 1