Sub topic 11
NULL in aggregates: COUNT, AVG, and SUM
This chapter assumes you can write basic SELECT statements and use COUNT, SUM, and AVG. It also assumes you understand that NULL represents a missing or unknown value, rather than zero or an empty string.
Aggregate functions summarize values from multiple rows. Oracle handles NULL differently depending on the function:
COUNT(*)counts rows, including rows whose columns containNULL.COUNT(expression)counts only rows where the expression is notNULL.SUMandAVGignoreNULLvalues.SUMandAVGreturnNULLwhen they have no non-NULLvalues.COUNTreturns0when it finds no matching values.
COUNT(*) and COUNT(expression)
Consider these rows:
| employee_id | commission_pct |
|---|---|
| 101 | 0.10 |
| 102 | NULL |
| 103 | 0.15 |
COUNT(*) counts all three rows. COUNT(commission_pct) counts only the two employees with known commission percentages.
SELECT COUNT(*) AS employee_rows,
COUNT(commission_pct) AS employees_with_commission
FROM employees;This distinction is useful when reporting data completeness. To count rows missing a value, subtract the non-NULL count from the total:
SELECT COUNT(*) - COUNT(commission_pct) AS missing_commission_count
FROM employees;COUNT(DISTINCT expression) also ignores NULL. It counts distinct non-NULL values.
SUM and AVG ignore NULL
Suppose commission percentages are known for only some employees. Oracle excludes missing percentages from both the sum and the average.
SELECT SUM(commission_pct) AS commission_total,
AVG(commission_pct) AS average_known_commission
FROM employees;If the values are 0.10, NULL, and 0.15, the results are:
SUM:0.25AVG:0.125, because Oracle divides by two known values, not three rows
This behavior is usually correct when NULL means “not recorded.” It would be incorrect to assume that missing means zero without a business rule saying so.
The following diagram shows how the three functions treat a NULL value.
Replacing NULL before aggregation
Use NVL when the report must treat missing values as a defined default. For example, this query treats a missing commission as zero:
SELECT AVG(commission_pct) AS average_earners,
AVG(NVL(commission_pct, 0)) AS average_all_employees
FROM employees;The first result describes the average among employees with a recorded commission. The second result describes the average across all employees, treating employees without commission as zero.
For totals, these two forms often produce the same result when rows exist:
SELECT SUM(NVL(commission_pct, 0)) AS total_commission,
NVL(SUM(commission_pct), 0) AS total_commission_defaulted
FROM employees;They express different intentions. The first changes each input value before summing. The second calculates the aggregate first and changes a NULL result afterward. The second form is often clearer when the requirement is “show zero when there is no total.”
NULL values within groups
With GROUP BY, each group applies the same rules independently. A department with employees but no known commissions has COUNT(*) greater than zero, while COUNT(commission_pct), SUM(commission_pct), and AVG(commission_pct) may indicate no known commission data.
SELECT department_id,
COUNT(*) AS employee_count,
COUNT(commission_pct) AS commission_count,
AVG(commission_pct) AS average_commission
FROM employees
GROUP BY department_id;Do not replace AVG with zero automatically. A displayed zero can mean “the average is zero,” while NULL can mean “there is not enough known data.”
Exercise
Which expression calculates the average commission among all employees while treating a missing commission as zero?
Show answer
AVG(NVL(commission_pct, 0)). AVG(commission_pct) excludes employees whose commission is NULL.