← Voltar ao Blog

Hierarchical Queries in SQL: Navigating Tree Structures with CONNECT BY and START WITH

Publicado em: 26/07/2026 16:45 SQL

📤 Compartilhe este artigo com o link curto:

💼 LinkedIn 🐦 X (Twitter) 👍 Facebook 💬 WhatsApp

Navigating Parent‑Child Relationships in Relational Databases

Understanding the order of execution of SQL queries is crucial to optimize performance and obtain accurate results efficiently. Knowing the sequence in which parts of an SQL query are executed

In enterprise systems, it is extremely common to encounter data that has a hierarchical parent‑child structure. Classic examples include corporate organograms (where an employee reports to a manager, who in turn reports to a director), product category trees, bills of materials (BOM) in manufacturing, or logistics route maps.

Querying these structures using only common JOINs can become a complex and rigid challenge, especially when the depth of the data tree is unknown or variable. To solve this problem with maximum elegance and performance, the Oracle database provides the native START WITH and CONNECT BY clauses.

The Concept of Relational Hierarchy

A table stores hierarchical data when it has a foreign key column that points to its own primary key (a reflexive or self‑referencing relationship). For example, in an employees table, we might have the columns:

The Fundamental Syntax of CONNECT BY

The structure of a hierarchical query in Oracle uses special directional operators to traverse the tree branches:

Practical corporate organogram example:

SELECT LEVEL AS nivel_hierarquico, LPAD(' ', 2 * (LEVEL - 1)) || full_name AS organograma_visual, job_title, employee_id, manager_id FROM employees START WITH manager_id IS NULL -- Starts at the top (e.g., the president who has no boss) CONNECT BY PRIOR employee_id = manager_id;

Auxiliary Functions for Hierarchical Analysis

Oracle provides built‑in pseudocolumns and functions that are extremely useful for enriching navigation through data trees:

Practical example using SYS_CONNECT_BY_PATH:

SELECT employee_id, full_name, SYS_CONNECT_BY_PATH(full_name, ' -> ') AS caminho_hierarquico FROM employees START WITH manager_id IS NULL CONNECT BY PRIOR employee_id = manager_id;

Avoiding Infinite Loops with NOCYCLE

In legacy or poorly maintained databases, data inconsistencies can cause an employee to point to themselves as a manager or create a closed cycle (where a subordinate is the boss of their own boss). When this occurs, a regular hierarchical query will fail with the well‑known ORA‑01436: CONNECT BY loop in user data error.

To protect your application against this scenario, always use the CONNECT BY NOCYCLE clause combined with safety checks:

SELECT employee_id, full_name FROM employees START WITH manager_id IS NULL CONNECT BY NOCYCLE PRIOR employee_id = manager_id;

Conclusion

Mastering hierarchical queries with START WITH and CONNECT BY empowers the developer to extract complex information from tree structures in a clean, fast, and structured way directly in the database.