This is the second part of a two-articles series. In the first part, we introduced the Common Table Expression (CTE), a new feature available on MySQL 8.0 as well as Percona Server for MySQL 8.0. In this article, we’ll present the Recursive Common Table Expression. SQL is generally poor at recursive structures, but it is now possible on MySQL to write recursive queries. Before MySQL 8.0, recursion was possible only by creating stored routines.
What is a Recursive Common Table Expression?
A recursive CTE is one having a subquery that refers to its own name. It is particularly useful in the following cases:
- To generate series
- Hierarchical or tree-structured data traversal