OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.2.2

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.2.2
    iconOceanBase Database
    SQL - V 4.2.2
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Hierarchical query

    Last Updated:2026-04-15 08:27:15  Updated
    share
    What is on this page
    Syntax
    Execution process
    Examples

    folded

    share

    A hierarchical query is a special query that can present hierarchical data in a hierarchical order.

    Hierarchical data refers to data with a hierarchical relationship in relational tables. This type of relationship is very common in our daily life. Here are some examples:

    • The relationship between team leaders and team members

    • The relationship between upper-level and lower-level departments in enterprises

    • The relationship between source and destination web pages in page jumping scenarios

    Syntax

    The syntax of SELECT for a simple query is as follows:

    SELECT [level], column, expr... FROM table [WHERE condition] [ START WITH start_expression ]
    CONNECT BY [NOCYCLE] { PRIOR child_expr = parent_expr | parent_expr = PRIOR child_expr }
    [ ORDER SIBLINGS BY ...] [ GROUP BY ... ] [ HAVING ... ] [ ORDER BY ... ]
    

    The following table describes the parameters in the syntax.

    Parameter
    Description
    level The level, namely, the hierarchy of the node. It is a pseudocolumn. Counting from the start point of a query, the level starts from 1, and so forth.
    CONNECT_BY_ISLEAF Indicates whether the current row is a leaf node. It is a pseudocolumn. Valid values:
    • 0: indicates that the current row is not a leaf node.
    • 1: indicates that the current row is a leaf node.
    CONNECT_BY_ISCYCLE Indicates whether the current row is in a cycle. It is a pseudocolumn. Valid values:
    • 0: indicates that the current row is not in a cycle.
    • 1: indicates that the current row is in a cycle.
    CONNECT_BY_ROOT CONNECT_BY_ROOT is a unary operator, indicating that the column in the parameter comes from the root node of the hierarchical query. It has the same precedence as the unary operators + and -.
    condition The condition.
    CONNECT BY Indicates how to determine the parent-child relationship. An equality expression is usually used. Other expressions are also supported.
    START WITH The root row in the hierarchical query.
    PRIOR PRIOR is a unary operator, indicating that the column in the parameter comes from the parent row. It has the same precedence as the unary operators + and -.
    NOCYCLE If this keyword is specified, the result can still be returned even if it contains a cycle. The CONNECT_BY_ISCYCLE pseudocolumn can be used to specify where the cycle occurs. Otherwise, an error is returned.
    ORDER SIBLINGS BY The sorting order for rows of siblings of the same parent.

    Execution process

    Before you take use of a hierarchical query, you must understand its execution process. The following describes the general execution process of a hierarchical query:

    1. Execute the SCAN or JOIN operation after the FROM clause.

    2. Generate a hierarchical relationship based on START WITH and CONNECT BY.

      The procedure for generating a hierarchical relationship is as follows:

      1. Obtain the root rows based on the expression in START WITH.

      2. Select the child rows of each root row based on the expression in CONNECT BY.

    3. Use the child rows generated in Step 2 as the new root rows and generate child rows for them. Repeat these steps cyclically until no new rows are generated.

    4. Execute the rest clauses such as WHERE, GROUP, and ORDER BY based on the general query execution process.

    Examples

    Create a table named emp and insert data into the emp_id, position, and mgr_id columns of the table.

    CREATE TABLE emp(emp_id INT,position VARCHAR(50),mgr_id INT);
    INSERT INTO emp VALUES (1,'Global manager',NULL);
    INSERT INTO emp VALUES (2,'Manager of European region',1);
    INSERT INTO emp VALUES (3,'Manager of Asia-Pacific region',1);
    INSERT INTO emp VALUES (4,'Manager of Americas region',1);
    INSERT INTO emp VALUES (5,'Manager of Italy region',2);
    INSERT INTO emp VALUES (6,'Manager of France region',2);
    INSERT INTO emp VALUES (7,'Manager of China region',3);
    INSERT INTO emp VALUES (8,'Manager of South Korea region',3);
    INSERT INTO emp VALUES (9,'Manager of Japan region',3);
    INSERT INTO emp VALUES (10,'Manager of US region',4);
    INSERT INTO emp VALUES (11,'Manager of Canada region',4);
    INSERT INTO emp VALUES (12,'Manager of Beijing, China region',7);
    

    In the preceding table, the position column has a clear hierarchical relationship.

    Execute the following statement to present the results in a hierarchical structure:

    obclient> SELECT emp_id, mgr_id, position, level FROM emp
        START WITH mgr_id IS NULL CONNECT BY PRIOR emp_id = mgr_id;
    +--------+--------+----------------------------------+-------+
    | EMP_ID | MGR_ID | POSITION                         | LEVEL |
    +--------+--------+----------------------------------+-------+
    |      1 |   NULL | Global manager                   |     1 |
    |      2 |      1 | Manager of Europe region         |     2 |
    |      5 |      2 | Manager of Italy region          |     3 |
    |      6 |      2 | Manager of France region         |     3 |
    |      3 |      1 | Manager of Asia-Pacific region   |     2 |
    |      7 |      3 | Manager of China region          |     3 |
    |     12 |      7 | Manager of Beijing, China region |     4 |
    |      8 |      3 | Manager of South Korea region    |     3 |
    |      9 |      3 | Manager of Japan region          |     3 |
    |      4 |      1 | Manager of Americas region       |     2 |
    |     10 |      4 | Manager of US region             |     3 |
    |     11 |      4 | Manager of Canada region         |     3 |
    +--------+--------+----------------------------------+-------+
    12 rows in set
    

    Execute the following statement to query the hierarchical structure of the Asia-Pacific region:

    obclient> SELECT emp_id, mgr_id, position, level FROM emp
        START WITH position = 'Manager of Asia-Pacific region' CONNECT BY PRIOR emp_id = mgr_id;
    +--------+--------+----------------------------------+-------+
    | EMP_ID | MGR_ID | POSITION                         | LEVEL |
    +--------+--------+----------------------------------+-------+
    |      3 |      1 | Manager of Asia-Pacific region   |     1 |
    |      7 |      3 | Manager of China region          |     2 |
    |     12 |      7 | Manager of Beijing, China region |     3 |
    |      8 |      3 | Manager of South Korea region    |     2 |
    |      9 |      3 | Manager of Japan region          |     2 |
    +--------+--------+----------------------------------+-------+
    5 rows in set
    

    Previous topic

    Simple query
    Last

    Next topic

    Compound query
    Next
    What is on this page
    Syntax
    Execution process
    Examples