Employees Earning More Than Their Managers


  • 0

    Click here to see the full article post


  • 0
    Z

    come and look,but there is a little complicate
    select E1.Name as Employee from Employee as E1
    inner join Employee as E2 on E1.ManagerId=E2.Id
    where E1.Salary>E2.Salary;


  • 0
    2

    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


  • 0
    P

    select e1.name as Employee from employee as e1
    left outer join Employee as e2
    on e1.managerid=e2.id
    where e1.salary>e2.salary;


  • 0
    M

    why I get the error "Column 'Name' in field list is ambiguous"?
    select (select a.Name from Employee as a, Employee as b
    where a.Salary > b.Salary
    and a.ManagerId = b.Id) as Employee


  • 0
    A

    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;
    

    The check

    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.


  • 0
    R
    SELECT
        NAME as Employee
    FROM
        Employee e1
    WHERE
        EXISTS (
                    SELECT 
                        Name
                    FROM 
                        Employee e2
                    WHERE
                        e2.id = e1.managerid
                        AND  e2.salary < e1.salary
                )
    

  • 0
    R

    Write your MySQL query statement below

    Select a.Name as Employee
    from Employee as a
    left join Employee as b on a.ManagerId=b.Id
    where a.Salary>b.Salary


  • 0
    D

    select name as Employee
    from Employee x
    where salary >
    (select salary
    from Employee y
    where x.ManagerId = y.Id);


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.