Find the downward recommendation chain for member ID 1

Question

Find the downward recommendation chain for member ID 1: that is, the members they recommended, the members those members recommended, and so on. Return member ID and name, and order by ascending member id.

Expected Results

memidfirstnamesurname
4JaniceJoplette
5GeraldButters
7NancyDare
10CharlesOwen
11DavidJones
14JackSmith
20MatthewGenting
21AnnaMackenzie
26DouglasJones
27HenriettaRumney

Your Answer

Your Results

Loading database...

Answers and Discussion

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.