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.2.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.2.0
    iconOceanBase Database
    SQL - V 4.2.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

    CORR

    Last Updated:2023-10-31 11:17:11  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

    CORR() returns the correlation coefficient of a set of numeric values. The correlation coefficient measures the strength of correlation between number pairs. The value range is [-1, 1]. The value 0 indicates that the number pairs are unrelated. A value less than 0 indicates negative correlation. A value greater than 0 indicates positive correlation. 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

    CORR(expr1, expr2) [ OVER (analytic_clause) ]
    

    Parameters

    Parameter Description
    expr1 The first parameter, which is of a numeric data type or a data type that can be implicitly converted to a numeric data type.
    expr2 The second parameter, which is of a numeric data type or a data type that can be implicitly converted to a numeric data type.
    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.

    Note

    • The database determines the parameter with the highest numeric precedence, implicitly converts the remaining parameter values to this data type, and returns a result of this data type.
    • The locations of the expr1 and expr2 parameters have no influence over the return result.
    • The return result is calculated by using the following formula: COVAR_POP(expr1, expr2) / (STDDEV_POP(expr1) * STDDEV_POP(expr2)).

    Return type

    If any parameter is NULL or you have only one row of data, NULL is returned. Otherwise, a value of the NUMBER type is returned.

    Examples

    Create a table named tbl1 and insert data into it.

    obclient> CREATE TABLE tbl1(col1 INT,col2 varchar(10),col3 INT,col4 INT);
    Query OK, 0 rows affected
    
    obclient> INSERT INTO tbl1 VALUES(1,'A1',8,12),(1,'A2',10,15),(1,'A3',11,16),
                (2,'B1',9,14),(2,'B2',10,15),(2,'B3',8,13),(2,'B4',11,16),(3,'C1',8,18),
                (3,'C2',9,16),(3,'C3',10,15),(3,'C4',11,12),(3,'C5',12,10);
    Query OK, 12 rows affected
    Records: 12  Duplicates: 0  Warnings: 0
    
    obclient> SELECT * FROM tbl1;
    +------+------+------+------+
    | COL1 | COL2 | COL3 | COL4 |
    +------+------+------+------+
    |    1 | A1   |    8 |   12 |
    |    1 | A2   |   10 |   15 |
    |    1 | A3   |   11 |   16 |
    |    2 | B1   |    9 |   14 |
    |    2 | B2   |   10 |   15 |
    |    2 | B3   |    8 |   13 |
    |    2 | B4   |   11 |   16 |
    |    3 | C1   |    8 |   18 |
    |    3 | C2   |    9 |   16 |
    |    3 | C3   |   10 |   15 |
    |    3 | C4   |   11 |   12 |
    |    3 | C5   |   12 |   10 |
    +------+------+------+------+
    12 rows in set
    

    Example of an aggregate function

    Calculate the correlation coefficient between the col3 column and the col4 column.

    obclient> SELECT CORR(col3,col4) FROM tbl1;
    +--------------------------------------------+
    | CORR(COL3,COL4)                            |
    +--------------------------------------------+
    | -.2705008904002296868793073195758520224002 |
    +--------------------------------------------+
    1 row in set
    

    Example of an analytic function

    Group data by the col1 column. Calculate the correlation coefficient between the col3 column and the col4 column.

    obclient> SELECT col1,col3,col4,CORR(col3,col4) OVER(PARTITION BY col1) "corr" FROM tbl1;
    +------+------+------+--------------------------------------------+
    | COL1 | COL3 | COL4 | corr                                       |
    +------+------+------+--------------------------------------------+
    |    1 |    8 |   12 |  .9958705948858223809835060513429288056548 |
    |    1 |   10 |   15 |  .9958705948858223809835060513429288056548 |
    |    1 |   11 |   16 |  .9958705948858223809835060513429288056548 |
    |    2 |    9 |   14 |                                          1 |
    |    2 |   10 |   15 |                                          1 |
    |    2 |    8 |   13 |                                          1 |
    |    2 |   11 |   16 |                                          1 |
    |    3 |    8 |   18 | -.9901475429766743091532731291244706579003 |
    |    3 |    9 |   16 | -.9901475429766743091532731291244706579003 |
    |    3 |   10 |   15 | -.9901475429766743091532731291244706579003 |
    |    3 |   11 |   12 | -.9901475429766743091532731291244706579003 |
    |    3 |   12 |   10 | -.9901475429766743091532731291244706579003 |
    +------+------+------+--------------------------------------------+
    12 rows in set
    

    Previous topic

    AVG
    Last

    Next topic

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