Click here to see the full article post
mssql has the key word 'with' which can create a temporary table
with A as (select *from Employee where ManagerId is not null),B as (select *from Employee where ManagerId is null)
select A.Name as Employee
from A cross join B
where A.ManagerId=B.Id and A.Salary>B.Salary
This is my solution as it was before I saw this post:
select E1.Name as Employee from Employee E1 join Employee E2 on E1.ManagerId = E2.Id where E1.Id in (select Id from Employee where ManagerId is not NULL) and E1.Salary > E2.salary;
where E1.Id in (select Id from Employee where ManagerId is not NULL)
seems redundant, but it improves performance hugely. I can't understand why is that.
SELECT NAME as Employee FROM Employee e1 WHERE EXISTS ( SELECT Name FROM Employee e2 WHERE e2.id = e1.managerid AND e2.salary < e1.salary )
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.