Self Join query

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

SELT JOINself JOIN is a regular join, but the table is joined with itself

Example 1

Table: Employee            
ID  NAME    Salary  ManagerID
1   Ram     70000   3
2   Shyam   80000   4
3   Sheeta  60000   NULL
4   Geeta   85000   NULL

Approach: 

Mysql: Earning of employees more than their managers

select e.ID, e.Name, e.Salary, e.ManagerID
from Employee e join 
Employee e1 on (e.ManagerId=e1.Id
where e.Salary>e1.Salary;

Result:
ID  NAME    Salary  ManagerID
1   Ram     70000   3


No comments:

Post a Comment