Jul 24, 2012

SQL Interview Questions and Answers: storing a tree structure in a database table

Q. How will you represent a hierarchical structure shown below in a relational database? or How will you store a tree data structure into DB tables?

A.The hierarchical data is an example of the composite design pattern. The entity relationship diagrams (aka ER diagram) are used to represent logical and physical relationships between the database tables. The diagram below shows how the table can be designed to store tree data by maintaining the adjacency information via superior_emp_id.

as you can see the "superior_emp_id" is a foreign key that points to the emp_id in the same table. So, Peter has null as he has no superiors. John and Amanda points to Peter who is their manager or superior and so on.

The above table can be created using SQL DDL (Data Definition Language) as shown below.

Q. Is there any other way to to store tree structure in a relational database?A. Yes, it can be done using the "modified preorder tree traversal" as described below.

As shown in the diagram above, each node is marked with a left and right numbers using a modified preorder traversal as shown above. This can be represented in a database table as shown below.

As you can see the numbers indicate the relationship between each node. All left values greater than 6 and right values less than 11 are descendants of 6-11 (i.e Id: 3 Amanda). Now if you want to extract out the 2-6 sub-tree for Amanda you can write the SQL as follows

SELECT * FROM employee WHERE left_val BETWEEN 6 and 11 ORDER BY left_val ASC;

Which will return Amanda, Ralph, and Jeanne.

If you want to get ancestors to a given node say 7-8 Ralph, you can write the SQL as follows

SELECT * FROM employee WHERE left_val < 7 and right_val > 8 WHERE ORDER BY left_val ASC;

Which will return: Peter and Amanda.

If you want to find out the number of descendants for a node, all you need is the left_val and right_val of the node for which you want to find the
descendants count. The formula is