Contents Index Using aggregate functions with DISTINCT The GROUP BY clause: organizing query results into groups

ASA SQL User's Guide
  Summarizing, Grouping and Sorting Query Results
    Summarizing query results using aggregate functions

Aggregate functions and NULL


Any NULLS in the column on which the aggregate function is operating are ignored for the purposes of the function except COUNT(*), which includes them. If all the values in a column are NULL, COUNT(column_name) returns 0.

If no rows meet the conditions specified in the WHERE clause, COUNT returns a value of 0. The other functions all return NULL. Here are examples:

SELECT COUNT (DISTINCT name)
FROM product
WHERE unit_price > 50
count(DISTINCT name)
0
SELECT AVG(unit_price)
FROM product
WHERE unit_price > 50
AVG(product.unit_price)
( NULL )

Contents Index Using aggregate functions with DISTINCT The GROUP BY clause: organizing query results into groups