Traversing tree-like data in a relational database using SQL

Celko's book is a good resource - if a bit overly "academic" at times.

I also have really found this method, known as 'closure tables' to work fairly well.

If you're using a database that allows recursive CTEs (such as PostgreSQL 8.4 or newer, or SQL Server 2005 or newer), they're really the best way to go. If you're on Oracle, there's always the venerable "connect by".

It is my experience that it's far more common to be handed a set of tables in a "naive tree" schema, and have to figure out how to extract the correct tree from that storage, than it is to have an opportunity to create the cleaner "closure tables" structure.


A recursive CTE is going to be your easiest solution. SQL Server 2005 and current versions of PostgreSQL support CTEs. If you're using SQL Server 2008 or newer, you could use the HIERARCHYID data type. You can find a good example of this at HierarchyID: Model Your Data Hierarchies with SQL Server 2008

Additional resources:

  • SQL Server 2008 – HierarchyID – Part I
  • SQL Server 2008 – HierarchyID – Part II
  • SQL Server 2008 Hierarchies and HierarchyID
  • The HierarchyID Datatype in SQL Server 2008

In SQL Server (2005 and later editions) you can use Common Table Expressions for reading hierarchies, see Microsoft SQL Server 2005 - CTE Example of a simple hierarchy for a couple of examples.

I have been recommended a book on the subject more generally which is "Trees and Hierarchies in SQL for Smarties" by Joe Celko - though I've not actually looked at the book myself yet.