Result of COUNT Function on NULL Values in DB2 Table



The COUNT function in DB2 is used to return the number of rows which satisfies the given criteria. The GROUP BY is used to divide the rows in the groups based on the criteria given in the query.

If we perform the GROUP BY on INVOICE_ID and there are few rows having NULL value in INVOICE_ID then the null values form a separate group. For example, if we have below table.

ORDER_ID INVOICE_ID
A11234 3214
A55611 3214
A99867 NULL
A55671 3214
A88907 NULL
A56012 6701

On executing the query which performs GROUP BY on INVOICE_ID and counts the number of rows, we will get the result below.

SELECT INVOICE_ID, COUNT(*) AS INVOICE COUNT FROM ORDERS GROUP BY INVOICE_ID;


INVOICE_ID INVOICE COUNT
3214 3
NULL 2
6701 1
Updated on: 2020-09-14T14:57:50+05:30

419 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements