I have a table named as employee
employee table fields and sample values
-------------------------------- id name parentid -------------------------------- 1 suresh null -------------------------------- 2 ram 1 -------------------------------- 3 ravi 2 -------------------------------- 4 raju 3 --------------------------------
Here I need to take the name field value of current, parent and grand parent values for given id value. I need the output as below
Assume the input id is 4 then my expected output is
name parentName grandParentName ----------------------------------------- raju ravi ram -----------------------------------------
How can I write query for this?
Good question! This is tested and works.