SummerDays
SummerDays

Reputation: 57

Combining 2 SQL statements into 1

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

Upvotes: 0

Views: 71

Answers (3)

mjpolak
mjpolak

Reputation: 769

Read about joining, for your example:

SELECT E.* 
FROM Employee E 
    JOIN Employee M ON E.ManagerId = M.Id 
WHERE E.Salary>M.Salary;

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

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

Upvotes: 2

Sparrow
Sparrow

Reputation: 2583

SELECT E.Salary, M.*
FROM Employee E
 inner join Mamanger M on E.ManagerId = M.Id and E.Salary > M.Salary

Upvotes: 3

Related Questions