Question: How do I execute hierarchical retrieval using CONNECT BY in Oracle?
To execute hierarchical retrieval using CONNECT BY in Oracle, you can use a recursive SQL query that uses the CONNECT BY clause to specify the hierarchical relationship between the rows in the table. The CONNECT BY clause defines the parent-child relationship by specifying the column that identifies the parent row and the column that identifies the child row.
Here are the steps to execute hierarchical retrieval using CONNECT BY in Oracle:
Define the table: Create a table that contains the data you want to retrieve hierarchically. The table should have a column that identifies the parent row and a column that identifies the child row.
Write the query: Write a recursive SQL query that uses the CONNECT BY clause to specify the hierarchical relationship between the rows in the table. The query should include the following elements:
The SELECT statement: This selects the columns you want to retrieve from the table.
The FROM clause: This specifies the table you want to retrieve data from.
The CONNECT BY clause: This specifies the parent-child relationship between the rows in the table.
The START WITH clause: This specifies the starting row for the hierarchy.
Execute the query: Run the SQL query in Oracle to retrieve the data hierarchically.
Here is an example of a query that retrieves hierarchical data using CONNECT BY:
SELECT employee_id, manager_id, first_name, last_name
CONNECT BY PRIOR employee_id = manager_id
START WITH manager_id IS NULL;
In this example, the query retrieves the employee_id, manager_id, first_name, and last_name columns from the employees table. The CONNECT BY clause specifies that the employee_id column is the child row and the manager_id column is the parent row. The PRIOR keyword specifies that the parent-child relationship is between the employee_id and manager_id columns. The START WITH clause specifies that the hierarchy starts with the rows where manager_id is null.
By following these steps, you can execute hierarchical retrieval using CONNECT BY in Oracle.
When one record is related to another record in the same table, you have a recursive relationship.
This kind of relationship is often called hierarchical.
A simple example of a hierarchical relationship is a family tree. In fact, because this is such an intuitive way of understanding a hierarchy, the relationship is often described as a parent/child relationship. A parent/child list is produced that lists all the "children" items under their "parent" item.
In our sample pet store schema, you can see an example of a hierarchical relationship in the PRODUCT table.
Some of the products are actually collections of other products. For example, the product called the Kitty Package is made up of a cat, a box of cat food, a bag of catnip, and a cat collar. Look at the following series of images below to see how the hierarchical relationship between the Kitty Package product and its four products works.
A new product record is added for cat. It has a PRODUCT_ID of 6.
Three other products are added with unique PRODUCT_IDs.
A new product (Kitty Package) is added with a PRODUCT_ID of 20. To connect the parent (Kitty Package) with the children
The final result is shown here in the PRODUCT table: Five Products that can all be purchased individually.
One of the challenges in working with hierarchical relationships is constructing a query that lists the parent with its children.
This is particularly difficult if the relationship has more than one level. Fortunately, Oracle has come up with a special feature you can use to write a query that uses the hierarchy tree to pull data out of the table. Here is the general syntax:
SELECT column_name, column_name, ...
CONNECT BY PRIOR parent_column_name =
START WITH condition;
The condition can be any valid condition you find in a WHERE clause.
For example, if you only want to see what is in the Puppy Package, this query will do it:
SELECT PRODUCT_NAME, PRODUCT_ID
CONNECT BY PRIOR PRODUCT_ID = PACKAGE_ID
START WITH PRODUCT_NAME = 'Puppy Package';
The special phrases, CONNECT BY, PRIOR, and START WITH are used especially for a hierarchical query.
Oracle also lets you see the hierarchy level by adding the LEVEL pseudocolumn to your query. The figure below shows the query and the results.
In the next lesson, you will compare the INTERSECT,MINUS, and UNION commands.