DB2 Version 9.7 for Linux, UNIX, and Windows

COUNT aggregate function

Read syntax diagramSkip visual syntax diagram
               .-ALL------.                    
>>-COUNT--(--+-+----------+--expression-+--)-------------------><
             | '-DISTINCT-'             |      
             '-*------------------------'      

The schema is SYSIBM.

The COUNT function returns the number of rows or values in a set of rows or values.

If DISTINCT is specified, the resulting data type of expression cannot be a BLOB, CLOB, DBCLOB, XML, distinct type on any of these types, or structured type (SQLSTATE 42907). Otherwise the result data type of expression can be any data type.

The result of the function is a large integer. The result cannot be null.

The argument of COUNT(*) is a set of rows. The result is the number of rows in the set. A row that includes only null values is included in the count.

The argument of COUNT(DISTINCT expression) is a set of values. The function is applied to the set of values derived from the argument values by the elimination of null and duplicate values. The result is the number of different non-null values in the set.

The argument of COUNT(expression) or COUNT(ALL expression) is a set of values. The function is applied to the set of values derived from the argument values by the elimination of null values. The result is the number of non-null values in the set, including duplicates.

Examples: