COUNT

functionsupportedCore SQL functions

docs.snowflake.com ↗

Aggregate functionsWindow functions

Returns either the number of non-NULL records for the specified columns, or the total number of records.

Syntax

supportedpartialnot supportednot scored

Aggregate function

COUNT( [ DISTINCT ] <expr1> [ , <expr2> ... ] )

COUNT(*)

COUNT(<alias>.*)

Window function

COUNT( [ DISTINCT ] <expr1> [ , <expr2> ... ] ) OVER (
                                                     [ PARTITION BY <expr3> ]
                                                     [ ORDER BY <expr4> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ <window_frame> ] ]
                                                     )

Parameters

NameRequiredDescription
expr1requiredA column name, which can be a qualified name (for example, database.schema.table.column_name).
expr2requiredYou can include additional column name(s) if you wish. For example, you could count the number of distinct combinations of last name and first name.
expr3requiredThe column to partition on, if you want the result to be split into multiple windows.
expr4requiredThe column to order each window on. Note that this is separate from any ORDER BY clause to order the final result set.
*requiredReturns the total number of records. When you pass a wildcard to the function, you can qualify the wildcard with the name or alias for the table. For example, to pass in all of the columns from the table named mytable, specify the following: Copy code (mytable.*) You can also use the ILIKE and EXCLUDE keywords for filtering: ILIKE filters for column names that match the specified pattern. Only one pattern is allowed. For example: Copy code (* ILIKE 'col1%') EXCLUDE filters out column names that don’t match the specified column or columns. For example: Copy code (* EXCLUDE col1) (* EXCLUDE (col1, col2)) Qualifiers are valid when you use these keywords. The following example uses the ILIKE keyword to filter for all of the columns that match the pattern col1% in the table mytable: Copy code (mytable.* ILIKE 'col1%') The ILIKE and EXCLUDE keywords can’t be combined in a single function call. If you specify an unqualified and unfiltered wildcard (*), the function returns the total number of records, including records with NULL values. If you specify a wildcard with the ILIKE or EXCLUDE keyword for filtering, the function excludes records with NULL values. For this function, the ILIKE and EXCLUDE keywords are valid only in a SELECT list or GROUP BY clause. For more information about the ILIKE and EXCLUDE keywords, see the “Parameters” section in SELECT.
alias.*requiredReturns the number of records that don’t contain any NULL values. For an example, see Examples.

Returns

Returns a value of type NUMBER.

Usage notes

See also

Referenced by