Skip to main content

APPROX_COUNT_DISTINCT (SQL)

.

Synopsis

APPROX_COUNT_DISTINCT(expression)

Description

APPROX_COUNT_DISTINCT is an aggregate function that returns an estimate of the number of rows that contain a unique value in the specified column. Use APPROX_COUNT_DISTINCT in a SELECT query to estimate how many unique values in columns from the table referenced in the query and return the count. This function uses the HyperLogLog algorithmOpens in a new tab in its estimation.

APPROX_COUNT_DISTINCT does not provide an precise count of the number of unique values, which you can instead obtain using COUNT(DISTINCT), but, due to the efficiency of the estimation process, executes orders of magnitude faster, particularly for large datasets.

Arguments

expression

A valid expression that contains the data values to be counted. expression can be the name of a column or an expression that evaluates to a column of data. You cannot specify expression as a subquery.

Examples

The following example returns the number of unique values in the Age column in the Sample.Customers table.

SELECT APPROX_COUNT_DISTINCT(Age) FROM Sample.Customers

See Also

FeedbackOpens in a new tab