Recursion in SQL

Relational database management systems based on SQL are only conditionally suitable for hierarchical or recursive queries. In this case other systems like ArangoDB are better (there was a great talk about this at the GOTO 2016). But you can also create recursive queries in SQL using Common Table Expressions and thus query e.g. classic parent/child tables for all ancestors and descendants.


In PostgreSQL, this would look like this:

0672c4761d202a35a9059fa137ace7c3

If you now want to get all children of a certain item_id, use the common table expressions:

0672c4761d202a35a9059fa137ace7c3

Also the query of the parents is no problem:

0672c4761d202a35a9059fa137ace7c3

Back