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.3.3

    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.3.3
    iconOceanBase Database
    SQL - V 4.3.3
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    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

    Subquery

    Last Updated:2024-12-02 03:48:26  Updated
    Share
    What is on this page
    Syntax
    Unnesting of nested subqueries
    Examples

    folded

    Share

    A subquery is a query nested within another SELECT statement and can return one row, multiple rows, or nothing. A SELECT statement can be nested with one or more other SELECT statements. A subquery in the FROM clause of a SELECT statement is also called an inner join view. You can nest any number of subqueries in a nested view. A subquery in the WHERE clause of a SELECT statement is also called a nested subquery.

    Subqueries can be divided into dependent subqueries and independent subqueries. A dependent subquery means that the execution of this subquery depends on variables of outer queries. Dependent queries are usually executed multiple times. An independent subquery means that the execution of this subquery does not depend on variables of outer queries. Independent subqueries are typically executed only once. You can rewrite independent subqueries and some of the dependent subqueries to unnest nested subqueries.

    Syntax

    The general syntax for subqueries is as follows:

    SELECT [ hint ] [ { { DISTINCT | UNIQUE } | ALL } ] select_list
    FROM { table_reference | join_clause | ( join_clause ) }
      [ , { table_reference | join_clause | (join_clause) } ]
      [ where_clause ]
      [ hierarchical_query_clause ]
      [ group_by_clause ]
    | subquery { UNION [ALL] | INTERSECT | MINUS } subquery [ { UNION [ALL] | INTERSECT | MINUS } subquery ]
    | ( subquery ) [ order_by_clause ] [ row_limiting_clause ]
    

    The following table describes the parameters in the syntax.

    Parameter
    Description
    select_list The select list.
    subquery The subquery.
    hint The comment.
    table_reference The table to be queried.

    If a column in a subquery has the same name as a column in the outer query, the table name or an alias must be prefixed to the duplicate column name in the outer query.

    When the outer query references a column in a subquery, the subquery is executed. The outer query can be the SELECT, UPDATE, or DELETE statement. The rules for referencing subqueries in different statements are as follows:

    • Define the row set to be inserted into the target table of the INSERT or CREATE TABLE statement.

    • Define the row set to be included in the view to be created in the CREATE VIEW or CREATE MATERIALIZED VIEW statement.

    • Define one or more values to be assigned to the existing rows in the UPDATE statement.

    • Specify conditions in the WHERE, HAVING, or START WITH clause.

    • Define a table that involves query operations.

    Unnesting of nested subqueries

    Subquery unnesting is a database optimization strategy. It places some subqueries in the outer parent query to convert some subqueries into equivalent multi-table join operations. This strategy can reduce the query layers as possible by using the access path, join method, and join order.

    OceanBase Database will unnest the following types of subqueries:

    • Independent IN subqueries

    • Dependent subqueries in IN and EXISTS that do not contain aggregate functions or the GROUP BY clause

    You can control whether to unnest the nested subqueries by using the UNNEST hint.

    Examples

    Create two tables respectively named table_a and table_b, and insert data into them.

    CREATE TABLE table_a(PK INT, name VARCHAR(25));
    INSERT INTO table_a VALUES(1,'Fox');
    INSERT INTO table_a VALUES(2,'Police');  
    INSERT INTO table_a VALUES(3,'Taxi');  
    INSERT INTO table_a VALUES(4,'Lincoln');  
    INSERT INTO table_a VALUES(5,'Arizona');  
    INSERT INTO table_a VALUES(6,'Washington');  
    INSERT INTO table_a VALUES(7,'Dell');  
    INSERT INTO table_a VALUES(10,'Lucent');
    CREATE TABLE table_b(PK INT, name VARCHAR(25));
    INSERT INTO table_b VALUES(1,'Fox');
    INSERT INTO table_b VALUES(2,'Police');  
    INSERT INTO table_b VALUES(3,'Taxi');  
    INSERT INTO table_b VALUES(6,'Washington');  
    INSERT INTO table_b VALUES(7,'Dell');  
    INSERT INTO table_b VALUES(8,'Microsoft');  
    INSERT INTO table_b VALUES(9,'Apple');
    INSERT INTO table_b VALUES(11,'Scotch whisky');
    
    • Independent subquery

      obclient> SELECT * FROM table_a t1 WHERE t1.PK IN (SELECT t2.PK FROM table_b t2);
      +------+-----------+
      | PK   | NAME      |
      +------+-----------+
      |    1 | Fox       |
      |    2 | Police    |
      |    3 | Taxi      |
      |    6 | Washington|
      |    7 | Dell      |
      +------+-----------+
      5 rows in set
      
    • Dependent subquery that uses outer query variable t1.PK

      SELECT * FROM table_a t1 WHERE t1.PK IN (SELECT t2.PK FROM table_b t2 WHERE t2.PK = t1.PK);
      +------+-----------+
      | PK   | NAME      |
      +------+-----------+
      |    1 | Fox       |
      |    2 | Police    |
      |    3 | Taxi      |
      |    6 | Washington|
      |    7 | Dell      |
      +------+-----------+
      5 rows in set
      
    • Dependent subquery that is unnested and rewritten as a join operation

      obclient> EXPLAIN SELECT * FROM table_a t1
        WHERE t1.PK IN (SELECT t2.name FROM table_b t2 WHERE t2.name = t1.name);
      +------------------------------------+
      | Query Plan                         |
      +------------------------------------+
      =============================================
      |ID|OPERATOR            |NAME|EST. ROWS|COST|
      ---------------------------------------------
      |0 |HASH RIGHT SEMI JOIN|    |8        |107 |
      |1 | TABLE SCAN         |T2  |8        |38  |
      |2 | TABLE SCAN         |T1  |8        |38  |
      =============================================
      Outputs & filters:
      -------------------------------------
        0 - output([T1.PK], [T1.NAME]), filter(nil),
            equal_conds([T1.PK = T2.NAME], [T2.NAME = T1.NAME]), other_conds(nil)
        1 - output([T2.NAME]), filter(nil),
            access([T2.NAME]), partitions(p0)
        2 - output([T1.NAME], [T1.PK]), filter(nil),
            access([T1.NAME], [T1.PK]), partitions(p0)
      +------------------------------------+
      1 row in set
      

    Previous topic

    Join query
    Last

    Next topic

    Lateral derived tables
    Next
    What is on this page
    Syntax
    Unnesting of nested subqueries
    Examples