Comment
Comment by nborwankar
This is related to a classic relational database problem called “bill of materials problem” - suggest you Google that. Also look for book on Graphs and networks in SQL by Joe Celko where he goes into modeling graphs in SQL tables.
Naive formulations will lead to deeply nested joins which balloon in computational cost.