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

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive 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.5
    iconOceanBase Database
    SQL - V 4.3.5
    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

    KEEP

    Last Updated:2026-04-09 02:53:55  Updated
    Share
    What is on this page
    Purpose
    Syntax
    Parameters
    Return type
    Examples
    Example of an aggregate function
    Example of an analytic function

    folded

    Share

    Purpose

    KEEP() manipulates values from a set of rows that rank based on a specified sorting rule and returns the first or last value. You can use this function as an aggregate or analytic function.

    Note

    • When you use this function as an analytic function, use the OVER clause to define a window over the data on which the function operates. This function divides the query result set into groups based on one or more expressions in query_partition_clause.
    • When you use this function as an aggregate function, the function operates on a set of rows and returns a single value. You do not need to add the OVER clause.

    Syntax

    aggregate_function KEEP ( DENSE_RANK { FIRST | LAST }
                    ORDER BY expr [ DESC | ASC ] [ NULLS { FIRST | LAST } ]
                          [, expr [ DESC | ASC ] [ NULLS { FIRST | LAST } ]]...
                            )
    [ OVER ( [query_partition_clause] ) ]
    
    aggregate_function:
      MIN
     |MAX
     |SUM
     |AVG
     |COUNT
     |VARIANCE
     |STDDEV
    

    Parameters

    Parameter
    Description
    aggregate_function The type of the aggregate function.
    expr An expression of any type.
    FIRST | LAST The position of the value to be returned after sorting by expr.
    • FIRST indicates to return the first value.
    • LAST indicates to return the last value.
    DESC | ASC Optional. The sorting method of the list.
    • ASC specifies to sort in ascending order. It is the default value.
    • DESC specifies to sort in descending order.
    NULLS { FIRST | LAST } Optional. The position of NULL in expr after sorting.
    • NULLS FIRST indicates that NULLs are sorted before non-NULL values.
    • NULLS LAST indicates that NULLs are sorted after non-NULL values.
    OVER You can use the OVER clause to define a window over the data on which the function operates. For more information, see Analytic functions.

    Return type

    This function returns a value of the same data type as the column specified in aggregate_function.

    Examples

    Create the employees table and insert data into it.

    obclient> CREATE TABLE employees (department_id INT,manager_id INT,last_name VARCHAR(50),hiredate VARCHAR(50),salary INT);
    Query OK, 0 rows affected
    
    obclient> INSERT INTO employees VALUES
        (30, 100, 'Raphaely', '2017-07-01', 1700),
        (30, 100, 'De Haan', '2018-05-01',11000),
        (40, 100, 'Errazuriz', '2017-07-21', 1400),
        (50, 100, 'Hartstein', '2019-10-05',14000),
        (50, 100, 'Raphaely', '2017-07-22', 1700),
        (50, 100, 'Weiss',  '2019-10-05',13500),
        (90, 100, 'Russell', '2019-07-11', 13000),
        (90,100, 'Partners',  '2018-12-01',14000);
    Query OK, 8 rows affected
    Records: 8  Duplicates: 0  Warnings: 0
    
    obclient> SELECT * FROM employees;
    +---------------+------------+-----------+------------+--------+
    | DEPARTMENT_ID | MANAGER_ID | LAST_NAME | HIREDATE   | SALARY |
    +---------------+------------+-----------+------------+--------+
    |            30 |        100 | Raphaely  | 2017-07-01 |   1700 |
    |            30 |        100 | De Haan   | 2018-05-01 |  11000 |
    |            40 |        100 | Errazuriz | 2017-07-21 |   1400 |
    |            50 |        100 | Hartstein | 2019-10-05 |  14000 |
    |            50 |        100 | Raphaely  | 2017-07-22 |   1700 |
    |            50 |        100 | Weiss     | 2019-10-05 |  13500 |
    |            90 |        100 | Russell   | 2019-07-11 |  13000 |
    |            90 |        100 | Partners  | 2018-12-01 |  14000 |
    +---------------+------------+-----------+------------+--------+
    8 rows in set
    

    Example of an aggregate function

    Sort the employees by their salaries and calculate the sum of the salary of the first employee, and then sort the employees by the hire date and calculate the sum of the salary of the last employee.

    obclient> SELECT SUM(salary) KEEP (DENSE_RANK FIRST ORDER BY salary) "Worst",
        SUM(salary) KEEP (DENSE_RANK LAST ORDER BY HIREDATE) "Best"
        FROM employees;
    +-------+-------+
    | Worst | Best  |
    +-------+-------+
    |  1400 | 27500 |
    +-------+-------+
    1 row in set
    

    Example of an analytic function

    For each department, sort the employees by their salaries and calculate the sum of the salary of the first employee, and then sort the employees by the hire date and calculate the sum of the salary of the last employee.

    obclient> SELECT last_name,department_id,salary,
        SUM(salary) KEEP (DENSE_RANK FIRST ORDER BY salary)
            OVER (PARTITION BY department_id) "Worst",
        SUM(salary) KEEP (DENSE_RANK LAST ORDER BY hiredate)
            OVER (PARTITION BY department_id) "Best"
        FROM employees
        ORDER BY department_id, salary, last_name;
    +-----------+---------------+--------+-------+-------+
    | LAST_NAME | DEPARTMENT_ID | SALARY | Worst | Best  |
    +-----------+---------------+--------+-------+-------+
    | Raphaely  |            30 |   1700 |  1700 | 11000 |
    | De Haan   |            30 |  11000 |  1700 | 11000 |
    | Errazuriz |            40 |   1400 |  1400 |  1400 |
    | Raphaely  |            50 |   1700 |  1700 | 27500 |
    | Weiss     |            50 |  13500 |  1700 | 27500 |
    | Hartstein |            50 |  14000 |  1700 | 27500 |
    | Russell   |            90 |  13000 | 13000 | 13000 |
    | Partners  |            90 |  14000 | 13000 | 13000 |
    +-----------+---------------+--------+-------+-------+
    8 rows in set
    

    Previous topic

    GROUPING
    Last

    Next topic

    LISTAGG
    Next
    What is on this page
    Purpose
    Syntax
    Parameters
    Return type
    Examples
    Example of an aggregate function
    Example of an analytic function