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

    COUNT

    Last Updated:2024-12-02 03:48:26  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

    COUNT() returns the number of rows of expr. 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. The function operates on a group of rows to return a list of values.
    • 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

    COUNT({ * | [ DISTINCT | UNIQUE | ALL ] expr }) [ OVER (analytic_clause) ]
    

    Parameters

    Parameter
    Description
    * Specifies to return all rows that meet the condition, including duplicate and NULL rows.
    DISTINCT | UNIQUE | ALL Specifies whether to remove duplicates during the execution of the query. This parameter is optional. Default value: ALL.
    • ALL: returns all values, including duplicate rows. Rows with NULLs are ignored.
    • DISTINCT: returns deduplicated rows. Rows with NULLs are ignored.
    • UNIQUE: returns deduplicated rows. Rows with NULLs are ignored.
    expr The name of the column to be calculated.
    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.

    Notice

    • The COUNT() function never returns NULL. If you specify expr, the function returns the number of rows where expr is not NULL. If you specify the asterisk (*), that is, COUNT(*), the function returns the number of all rows. When the DISTINCT, UNIQUE, or ALL parameter is used, separate them from expr with a space.
    • If you specify the DISTINCT or UNIQUE keyword, you can specify only analytic_clause. order_by_clause and windowing_clause are not allowed.

    Return type

    This function returns a natural number.

    Examples

    Create the tbl1 table and insert test data into the table.

    obclient> CREATE TABLE tbl1 (col1 INT,col2 VARCHAR(10),col3 INT);
    Query OK, 0 rows affected
    
    obclient> INSERT INTO tbl1 VALUES
        (1,'aa',10),(2,'bb',12),(3,'cc',15),(4,'dd',18),(5,'ee',20),
        (6,'ff',23),(7,'gg',25),(8,'hh',30),(9,'ii',40),(10,'jj',15),
        (11,'kk',NULL),(12,'oo',NULL);
    Query OK, 12 rows affected
    Records: 12  Duplicates: 0  Warnings: 0
    
    obclient> SELECT * FROM tbl1;
    +------+------+------+
    | COL1 | COL2 | COL3 |
    +------+------+------+
    |    1 | aa   |   10 |
    |    2 | bb   |   12 |
    |    3 | cc   |   15 |
    |    4 | dd   |   18 |
    |    5 | ee   |   20 |
    |    6 | ff   |   23 |
    |    7 | gg   |   25 |
    |    8 | hh   |   30 |
    |    9 | ii   |   40 |
    |   10 | jj   |   15 |
    |   11 | kk   | NULL |
    |   12 | oo   | NULL |
    +------+------+------+
    12 rows in set
    

    Example of an aggregate function

    Obtain the number of non-NULL rows in the col3 column in the tbl1 table and the total number of rows in tbl1.

    obclient> SELECT COUNT(col3),COUNT(*) FROM tbl1;
    +-------------+----------+
    | COUNT(COL3) | COUNT(*) |
    +-------------+----------+
    |          10 |       12 |
    +-------------+----------+
    1 row in set
    

    Example of an analytic function

    Return the number of values with an offset within the range between 1 and 3 in the ordered col3 column in table tbl1.

    obclient> SELECT col1,col2,
                     COUNT(*) OVER (ORDER BY col3 RANGE BETWEEN 1 PRECEDING AND 3 FOLLOWING) AS mov_count
              FROM tbl1;
    +------+------+-----------+
    | COL1 | COL2 | MOV_COUNT |
    +------+------+-----------+
    |    1 | aa   |         2 |
    |    2 | bb   |         3 |
    |    3 | cc   |         3 |
    |   10 | jj   |         3 |
    |    4 | dd   |         2 |
    |    5 | ee   |         2 |
    |    6 | ff   |         2 |
    |    7 | gg   |         1 |
    |    8 | hh   |         1 |
    |    9 | ii   |         1 |
    |   11 | kk   |         2 |
    |   12 | oo   |         2 |
    +------+------+-----------+
    12 rows in set
    

    Previous topic

    CORR
    Last

    Next topic

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