Solution: Use Alternative Tree Models

You can use any one of the following solutions to work with hierarchical data in SQL. Each has strengths and weaknesses, and any of them might be the right choice for a given application.

Recursive Queries

Some brands of RDBMS implement SQL syntax features to support hierarchies stored in the Adjacency List format. The SQL-99 standard defines recursive query syntax using the WITH keyword followed by a recursive common table expression.

​ ​WITH​ RECURSIVE CommentTree
​  (comment_id, bug_id, parent_id, author, comment_date, ​comment​, depth)
​ ​AS​ (
​  ​SELECT​ comment_id, bug_id, parent_id, author, comment_date,
​  ​comment​, 0 ​AS​ depth
​  ​FROM ...

Get SQL Antipatterns, Volume 1 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.