DB2 10.5 for Linux, UNIX, and Windows

COUNT aggregate function

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

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

The schema is SYSIBM.

expression

If ALL is implied or specified, an expression that returns a value of any built-in data type. If DISTINCT is specified, an expression that returns a value of any built-in data type except BLOB, CLOB, DBCLOB, or XML.

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