Thanks. But it is taking too much time when trying to fetch the same for single studentid (say 25) from a 500 Million table. Seems all the hierarchies are being built out of 500 Million rows, then it's fetching record for studentId (say 25). Where this studentid = 25 can be introduced as a filter inside the query so that query plan is optimum and it only fetches the hierachy corresponding to 25.