Assume that a tree structure is implemented in SQL:
CREATE TABLE nodes ( id INTEGER PRIMARY KEY, parent INTEGER
Although loops can be created in this view, suppose we never allow this. Only the collection of roots (records where the parent is null) and their descendants will be stored in the table.
The goal is to find all nodes that are descendants based on the node identifier in the table.
A is a descendant of B if the parent A B or A strong> parent is a descendant of B. Pay attention to the recursive definition.
Here are some sample data:
INSERT INTO nodes VALUES (1, NULL); INSERT INTO nodes VALUES (2, 1); INSERT INTO nodes VALUES (3, 2); INSERT INTO nodes VALUES (4, 3); INSERT INTO nodes VALUES (5, 3); INSERT INTO nodes VALUES (6, 2);
which represents:
1 `-- 2 |-- 3 | |-- 4 | `-- 5 | `-- 6
We can select (immediate) children 1 by doing the following:
SELECT a.* FROM nodes AS a WHERE parent=1;
We can select children and grandchildren 1 by following these steps:
SELECT a.* FROM nodes AS a WHERE parent=1 UNION ALL SELECT b.* FROM nodes AS a, nodes AS b WHERE a.parent=1 AND b.parent=a.id;
We can choose children, grandchildren and great-grandchildren 1 by doing this:
SELECT a.* FROM nodes AS a WHERE parent=1 UNION ALL SELECT b.* FROM nodes AS a, nodes AS b WHERE a.parent=1 AND b.parent=a.id UNION ALL SELECT c.* FROM nodes AS a, nodes AS b, nodes AS c WHERE a.parent=1 AND b.parent=a.id AND c.parent=b.id;
How can I build a query that receives all descendants of node 1 , and not at a fixed depth? It looks like I will need to create a recursive query or something else.
I would like to know if such a query is possible with SQLite. However, if this type of query requires functions that are not available in SQLite, I am curious to know if this can be done in other SQL databases.