0

I want to select employees who earns more than their managers. I have these SQL statements I wrote below, but how exactly would I combine these to make one statement?

SELECT E.Salary
FROM Employee E
WHERE E.ManagerId = E.Id

SELECT *
FROM Employee M
WHERE M.Salary > E.Salary AND M.ManagerId != M.Id
SummerDays
  • 65
  • 2
  • 10
  • Your first query returns salaries of people who manage themselves, which is not a part of selecting "employees who earns more than their managers". Your second query is not a valid SQL statement since E is undefined. PS See [this](http://stackoverflow.com/a/33952141/3404097) re querying. – philipxy Oct 14 '16 at 17:25

3 Answers3

3
SELECT E.Salary, M.*
FROM Employee E
 inner join Mamanger M on E.ManagerId = M.Id and E.Salary > M.Salary
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sparrow
  • 2,548
  • 1
  • 24
  • 28
2

you could use a self inner join

SELECT E.*
FROM Employee E
INNER JOIN Employee M ON  E.ManagerId = M.Id
WHERE E.Salary > M.Salary
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

Read about joining, for your example:

SELECT E.* 
FROM Employee E 
    JOIN Employee M ON E.ManagerId = M.Id 
WHERE E.Salary>M.Salary;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
mjpolak
  • 721
  • 6
  • 24