with recursive recommendeds(memid) as ( select memid from cd.members where recommendedby = 1 union all select mems.memid from recommendeds recs inner join cd.members mems on mems.recommendedby = recs.memid ) select recs.memid, mems.firstname, mems.surname from recommendeds recs inner join cd.members mems on recs.memid = mems.memid order by memid
This is a pretty minor variation on the previous question. The essential difference is that we're now heading in the opposite direction. One interesting point to note is that unlike the previous example, this CTE produces multiple rows per iteration, by virtue of the fact that we're heading down the recommendation tree (following all branches) rather than up it.