OceanBase logo

OceanBase

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

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

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

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.4.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 & Certification
    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.4.2
    iconOceanBase Database
    SQL - V 4.4.2
    SQL
    KV
    • 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

    UNION clause

    Last Updated:2026-04-02 06:23:58  Updated
    share
    What is on this page
    Purpose
    Limitations and considerations
    Syntax
    Parameters
    Examples
    References

    folded

    share

    Purpose

    The statement is used to perform set operations on the results of multiple SELECT queries.

    Limitations and considerations

    • Set operators have the same precedence. If an SQL statement contains multiple set operators, OceanBase Database will calculate them from left to right unless parentheses are used to specify the order of operations.

    • Each set operator can operate only on the result set of a SELECT statement, and the result sets must have the same number of columns and data types.

    Syntax

    select_stmt
        {UNION | UNION ALL | MINUS | EXCEPT | INTERSECT} select_stmt 
        [, {UNION | UNION ALL | MINUS | EXCEPT | INTERSECT} select_stmt ...]
        [ORDER BY order_by_condition_list]
        [LIMIT limit_clause];
    

    Parameters

    Parameter Description
    select_stmt The SELECT statement to perform set operations on. For more information about the SELECT statement, see SELECT.
    UNION The operator used to merge the result sets of two SELECT statements into one set, and remove duplicate rows.
    UNION ALL The operator used to merge the result sets of two SELECT statements into one set without removing duplicate rows.
    MINUS | EXCEPT The operator used to return the rows in the result set of the preceding SELECT statement that are not in the result set of the following SELECT statement. MINUS is synonymous with EXCEPT.
    INTERSECT The operator used to return the intersection of the result sets of two SELECT statements, and remove duplicate rows.
    ORDER BY order_by_condition_list The clause used to specify the order of the rows after set operations are performed.
    LIMIT limit_clause The clause used to specify the number of rows to return after set operations are performed.

    Examples

    1. Create tables test_tbl1 and test_tbl2.

       CREATE TABLE test_tbl1 (col1 INT, col2 INT);
       CREATE TABLE test_tbl2 (col1 INT, col2 INT);
      
    2. Insert test data into tables test_tbl1 and test_tbl2.

       INSERT INTO test_tbl1 VALUES (1, 1), (2, 2), (4, 4);
       INSERT INTO test_tbl2 VALUES (2, 2), (3, 3), (5, 5);
      
    • Select the data from the col1 and col2 columns of tables test_tbl1 and test_tbl2, and merge the results into one set using the UNION ALL operator. Duplicate rows are retained.

        SELECT col1, col2 FROM test_tbl1 
        UNION ALL 
        SELECT col1, col2 FROM test_tbl2;
      

      The return result is as follows:

        +------+------+
        | col1 | col2 |
        +------+------+
        |    1 |    1 |
        |    2 |    2 |
        |    4 |    4 |
        |    2 |    2 |
        |    3 |    3 |
        |    5 |    5 |
        +------+------+
        6 rows in set
      
    • Select the data from the col1 and col2 columns of tables test_tbl1 and test_tbl2, and merge the results into one set using the UNION operator. Duplicate rows are removed.

        SELECT col1, col2 FROM test_tbl1 
        UNION 
        SELECT col1, col2 FROM test_tbl2;
      

      The return result is as follows:

        +------+------+
        | col1 | col2 |
        +------+------+
        |    1 |    1 |
        |    2 |    2 |
        |    4 |    4 |
        |    3 |    3 |
        |    5 |    5 |
        +------+------+
        5 rows in set
      
    • Select the data from the col1 and col2 columns of tables test_tbl1 and test_tbl2, and return the intersection of the two sets. That is, return the duplicate rows in the col1 and col2 columns of test_tbl1 and test_tbl2.

        SELECT col1, col2 FROM test_tbl1 
        INTERSECT
        SELECT col1, col2 FROM test_tbl2;
      

      The return result is as follows:

        +------+------+
        | col1 | col2 |
        +------+------+
        |    2 |    2 |
        +------+------+
        1 row in set
      
    • Select the data from the col1 and col2 columns of test_tbl1, and exclude the data from the col1 and col2 columns of test_tbl2. That is, return the rows in test_tbl1 that do not exist in test_tbl2.

        SELECT col1, col2 FROM test_tbl1 
        EXCEPT
        SELECT col1, col2 FROM test_tbl2;
      

      The return result is as follows:

        +------+------+
        | col1 | col2 |
        +------+------+
        |    1 |    1 |
        |    4 |    4 |
        +------+------+
        2 rows in set
      
    • Obtain the data from the col1 and col2 columns of test_tbl1 and test_tbl2, merge the data, sort the merged data in descending order by col1, and return the first three rows.

        SELECT col1, col2 FROM test_tbl1 
        UNION
        SELECT col1, col2 FROM test_tbl2
        ORDER BY col1 DESC
        LIMIT 3;
      

      The return result is as follows:

        +------+------+
        | col1 | col2 |
        +------+------+
        |    5 |    5 |
        |    4 |    4 |
        |    3 |    3 |
        +------+------+
        3 rows in set
      

    References

    • SELECT
    • Set operations

    Previous topic

    WINDOW clause
    Last

    Next topic

    SET DEFAULT ROLE
    Next
    What is on this page
    Purpose
    Limitations and considerations
    Syntax
    Parameters
    Examples
    References