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

    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.3.0
    iconOceanBase Database
    SQL - V 4.3.0
    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

    APPROX_COUNT_DISTINCT

    Last Updated:2026-04-15 08:30:01  Updated
    share
    What is on this page
    Purpose
    Syntax
    Parameters
    Return type
    Examples

    folded

    share

    Purpose

    APPROX_COUNT_DISTINCT() returns an approximate number of unique rows in a column. You can use this function to further calculate the selectivity of referenced columns.

    Compared with the COUNT(DISTINCT x) function, APPROX_COUNT_DISTINCT() returns an approximate value, and therefore has an extremely fast calculation speed. When processing a large amount of data, COUNT(DISTINCT x) often takes a long time. APPROX_COUNT_DISTINCT, however, greatly improves the computing efficiency at a slight cost of accuracy.

    Syntax

    APPROX_COUNT_DISTINCT(expr)
    

    Parameters

    expr indicates the column to deduplicate.

    Return type

    The return type is NUMBER.

    Examples

    Create a table named tbl1 and insert 10 data entries into it.

    obclient> CREATE TABLE tbl1 (col1 INT,col2 INT,col3 varchar(10));
    Query OK, 0 rows affected
    
    obclient> INSERT INTO tbl1 VALUES (1,10,'a'),(2,20,'b'),(3,30,'c'),
        (4,40,'a'),(5,50,'c'),(1,10,'a'),(2,20,'b'),(3,30,'c'),(4,30,'a'),(5,40,'b');
    Query OK, 10 rows affected
    Records: 10  Duplicates: 0  Warnings: 0
    
    • Return the number of unique values in the col2 column.

      obclient> SELECT APPROX_COUNT_DISTINCT(col2) FROM tbl1;
      +-----------------------------+
      | APPROX_COUNT_DISTINCT(COL2) |
      +-----------------------------+
      |                           5 |
      +-----------------------------+
      1 row in set
      
    • Group and deduplicate the data records by the col1 and col2 columns, and count the number of each value in the col1 column.

      obclient> SELECT col1,APPROX_COUNT_DISTINCT(col2) FROM tbl1 GROUP BY col1;
      +------+-----------------------------+
      | COL1 | APPROX_COUNT_DISTINCT(COL2) |
      +------+-----------------------------+
      |    1 |                           1 |
      |    2 |                           1 |
      |    3 |                           1 |
      |    4 |                           2 |
      |    5 |                           2 |
      +------+-----------------------------+
      5 rows in set
      
    • Group and deduplicate the data records by the col1, col2, and col3 columns, and count the number of each value pair in the col1 and col3 columns.

      obclient> SELECT col1,col3,APPROX_COUNT_DISTINCT(col2) FROM tbl1 GROUP BY col1,col3;
      +------+------+-----------------------------+
      | COL1 | COL3 | APPROX_COUNT_DISTINCT(COL2) |
      +------+------+-----------------------------+
      |    1 | a    |                           1 |
      |    2 | b    |                           1 |
      |    3 | c    |                           1 |
      |    4 | a    |                           2 |
      |    5 | b    |                           1 |
      |    5 | c    |                           1 |
      +------+------+-----------------------------+
      6 rows in set
      

    Previous topic

    UPDATEXML
    Last

    Next topic

    AVG
    Next
    What is on this page
    Purpose
    Syntax
    Parameters
    Return type
    Examples