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

    Lateral derived tables

    Last Updated:2024-12-02 03:48:29  Updated
    Share
    What is on this page
    Concept
    Syntax
    Examples
    Create a test table and add test data to the table
    Query the test data
    References

    folded

    Share

    This topic describes the basic concept and syntax of lateral derived tables, and provides some examples to introduce how to use lateral derived tables.

    Concept

    A derived table is a subquery used in a FROM clause. The result set of this subquery is temporarily used as a table in outer queries. A derived table is usually used to group and aggregate data, or used to create a dataset that meets the specified conditions for use in the main query.

    Lateral derived tables are a special type of derived tables. You can use the LATERAL keyword to specify a lateral derived table so that it can reference fields in another table or derived table previously defined in the same FROM clause. This way, a subquery in the lateral derived table can reference tables defined in the same FROM clause and access column values in these tables.

    A lateral derived table differs from a normal derived table in that it can reference columns in tables that are previously defined in the same FROM clause.

    Syntax

    SELECT select_list
    FROM table_name1,
    LATERAL (SELECT select_list
             FROM table_name2
             WHERE table_name2.col_name = table_name1.col_name) AS lateral_derived_table_name
    [...];
    

    The parameters are described as follows:

    Parameter
    Description
    select_list The list of columns to retrieve, which can be column names, expressions, or aggregate functions. Separate multiple columns with commas (,).
    table_name1 The primary table to query.
    LATERAL A lateral join subquery, namely, a lateral derived table.
    table_name2 A secondary table referenced in the LATERAL subquery to provide related additional information for each row in the primary table specified by table_name1.
    lateral_derived_table_name The alias of the secondary table, which is referenced in subsequent queries.
    [...] An optional query clause, such as a WHERE clause.

    Examples

    Create a test table and add test data to the table

    1. Create a table named students.

      CREATE TABLE students (
        id NUMBER PRIMARY KEY,
        name VARCHAR2(50) NOT NULL,
        age NUMBER
      );
      
    2. Insert 3 data records into the students table.

      INSERT INTO students
        VALUES (1, 'name1', 20),
        (2, 'name2', 22),
        (3, 'name3', 21);
      
    3. Create a table named scores.

      CREATE TABLE scores (
        id NUMBER PRIMARY KEY,
        student_id NUMBER,
        subject VARCHAR2(50) NOT NULL,
        score DECIMAL(5, 2),
        FOREIGN KEY (student_id) REFERENCES students(id)
      );
      
    4. Insert 9 data records into the scores table.

      INSERT INTO scores
        VALUES (1, 1, 'A', 86.5),
        (2, 1, 'B', 90.0),
        (3, 1, 'C', 91.5),
        (4, 2, 'A', 86.0),
        (5, 2, 'B', 92.0),
        (6, 2, 'C', 89.5),
        (7, 3, 'A', 93.0),
        (8, 3, 'B', 92.5),
        (9, 3, 'C', 85.0);
      

    Query the test data

    Query the students and scores tables for the name, average score, and highest score of each student.

    Q1: Use multiple subqueries to obtain required data. Perform a group operation and an aggregate operation on the scores table. Scan the scores table twice to calculate the average score and highest score of each student. Then, use the WHERE clause to join the results with the students table.

    SELECT st.name, sc.avg_score, scs.max_score
    FROM students st,
      (SELECT student_id, AVG(score) avg_score
       FROM scores
       GROUP BY student_id) sc,
       (SELECT student_id, MAX(score) max_score
       FROM scores
       GROUP BY student_id) scs
    WHERE sc.student_id = st.id
    AND scs.student_id = st.id;
    

    The result is as follows:

    +-------+-------------------------------------------+-----------+
    | NAME  | AVG_SCORE                                 | MAX_SCORE |
    +-------+-------------------------------------------+-----------+
    | name1 | 89.33333333333333333333333333333333333333 |      91.5 |
    | name2 | 89.16666666666666666666666666666666666667 |        92 |
    | name3 | 90.16666666666666666666666666666666666667 |        93 |
    +-------+-------------------------------------------+-----------+
    3 rows in set
    

    Q2: Use the LATERAL keyword to obtain required data. Use the LATERAL keyword (lateral derived table) to calculate the average score and highest score of each student in an SQL statement, and scan the scores table once for the ID of each student.

    SELECT st.name, ld_tbl.avg_score, ld_tbl.max_score
    FROM students st,
      LATERAL (SELECT AVG(score) avg_score, MAX(score) max_score
               FROM scores sc
               WHERE sc.student_id = st.id) ld_tbl;
    

    The result is as follows:

    +-------+-------------------------------------------+-----------+
    | NAME  | AVG_SCORE                                 | MAX_SCORE |
    +-------+-------------------------------------------+-----------+
    | name1 | 89.33333333333333333333333333333333333333 |      91.5 |
    | name2 | 89.16666666666666666666666666666666666667 |        92 |
    | name3 | 90.16666666666666666666666666666666666667 |        93 |
    +-------+-------------------------------------------+-----------+
    3 rows in set
    

    Q2 has higher performance than Q1 in processing a large dataset because it avoids repeatedly scanning the scores table. The syntax of Q2 is clearer and more compact and easier to understand and maintain.

    References

    • SELECT

    • Use aggregate functions in queries

    Previous topic

    Subqueries
    Last

    Next topic

    Use arithmetic operators in queries
    Next
    What is on this page
    Concept
    Syntax
    Examples
    Create a test table and add test data to the table
    Query the test data
    References