COUNT
functionsupportedCore SQL functionsReturns 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
| Name | Required | Description |
|---|---|---|
expr1 | required | A column name, which can be a qualified name (for example, database.schema.table.column_name). |
expr2 | required | You 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. |
expr3 | required | The column to partition on, if you want the result to be split into multiple windows. |
expr4 | required | The column to order each window on. Note that this is separate from any ORDER BY clause to order the final result set. |
* | required | Returns 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.* | required | Returns 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
- This function treats JSON null (VARIANT NULL) as SQL NULL.
- For more information about NULL values and aggregate functions, see Aggregate functions and NULL values.
- When this function is called as an aggregate function: If the DISTINCT keyword is used, it applies to all columns. For example, DISTINCT col1, col2, col3 means to return the number of different combinations of columns col1, col2, and col3. For example, assume the data is: Copy code 1, 1, 1 1, 1, 1 1, 1, 1 1, 1, 2 In this case, the function returns 2, because that’s the number of distinct combinations of values in the three columns.
- When this function is called as a window function with an OVER clause that contains an ORDER BY clause: A window frame is required. If no window frame is specified explicitly, the following implied window frame is used: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW For more information about window frames, including syntax, usage notes, and examples, see Window function syntax and usage.
- DISTINCT can be used with an OVER clause that includes ORDER BY when the distinct set applies to a single column; for example, COUNT(DISTINCT <expr>) OVER ( ... ORDER BY <orderexpr> [ ... ] ). A distinct count that references multiple column expressions, such as COUNT(DISTINCT <expr1>, <expr2>, ... ) in a window, is not valid when the OVER clause also contains ORDER BY.
- To return the number of rows that match a condition, use COUNT_IF.
- When possible, use the COUNT function on tables and views without a row access policy. The query with this function is faster and more accurate on tables or views without a row access policy. The reasons for the performance difference include: Snowflake maintains statistics on tables and views, and this optimization allows simple queries to run faster.
- When a row access policy is set on a table or view and the COUNT function is used in a query, Snowflake must scan each row and determine whether the user is allowed to view the row.