Skip to content
Advertisement

How self join works [closed]

I have a customer table:

ID  Name  Referred_id  
1   aaa
2   bbb   1
3   ccc   2
4   ddd   2
5   eee   4

I want to have a new column referred_name that should show who referred which customer based on referred_id, the output should be:

ID  Name  Referred_id  Referred_name
1   aaa
2   bbb   1               aaa
3   ccc   2               bbb
4   ddd   2               bbb
5   eee   4               ddd

Can someone please help me with the query

Thanks

Advertisement

Answer

SELECT t1.*, t2.Name 'Referred_name' FROM Customer t1 LEFT JOIN Customer t2 ON t1.Referred_id = t2.ID
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement