Sub topic 2
How Aggregates and Groups Treat NULL
Prerequisites
Assume you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also know that NULL represents a missing or unknown value. NULL is not the same as zero, an empty string, or the value FALSE.
Aggregate functions calculate one result from several rows. Common aggregate functions include COUNT, SUM, AVG, MIN, and MAX.
Aggregate functions skip NULL values
Most aggregate functions ignore NULL input values. They calculate from the remaining non-NULL values.
| Function | Behavior with NULL input |
|---|---|
| `COUNT(*)` | Counts every input row, including rows containing NULL |
| `COUNT(column)` | Counts only rows where `column` is not NULL |
| `SUM(column)` | Adds non-NULL values |
| `AVG(column)` | Averages non-NULL values |
| `MIN(column)` and `MAX(column)` | Find the minimum or maximum non-NULL value |
Consider this complete query:
WITH orders(order_id, amount, shipped_at) AS (
VALUES
(1, 100.00, '2026-01-10'),
(2, NULL, '2026-01-11'),
(3, 50.00, NULL)
)
SELECT COUNT(*) AS row_count,
COUNT(amount) AS amount_count,
SUM(amount) AS total_amount,
AVG(amount) AS average_amount,
MIN(amount) AS smallest_amount,
MAX(amount) AS largest_amount
FROM orders;The result has three input rows, but only two non-NULL amounts. The sum is 150.00, and the average is 75.00. The missing amount does not act like zero. If it acted like zero, the average would incorrectly be 50.00.
COUNT(*) counts rows because it does not inspect a column. COUNT(amount) counts values, so it skips the row whose amount is NULL.
Empty input and all-NULL input
For SUM, AVG, MIN, and MAX, a group with no non-NULL values produces NULL. COUNT produces zero.
This is useful because NULL can tell you that no usable value existed. If your report needs zero instead, convert the result after aggregation:
WITH payments(amount) AS (
VALUES
(CAST(NULL AS DECIMAL(10, 2))),
(CAST(NULL AS DECIMAL(10, 2)))
)
SELECT COALESCE(SUM(amount), 0) AS reported_total
FROM payments;COALESCE returns the first non-NULL expression. Use it deliberately. A missing total and a confirmed total of zero can have different meanings.
NULL values form one group
GROUP BY places rows with NULL in the same group. SQL treats all NULL grouping keys as one grouping category, even though comparisons such as NULL = NULL do not evaluate to true.
WITH orders(customer_id, amount) AS (
VALUES
(1, 100.00),
(NULL, 25.00),
(NULL, 40.00)
)
SELECT customer_id,
COUNT(*) AS order_count,
SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id;This query returns one group for customer 1 and one group whose customer_id is NULL. The NULL group contains two orders and has a total of 65.00.
Check your interpretation
When reviewing a grouped query, ask whether NULL means “unknown,” “not applicable,” or “not recorded.” Those meanings affect whether you should skip the value, replace it, or report it separately.
Exercise
In a group containing amounts 10, NULL, and 20, what are COUNT(*), COUNT(amount), SUM(amount), and AVG(amount)?
Show answer
COUNT(*) is 3, COUNT(amount) is 2, SUM(amount) is 30, and AVG(amount) is 15. The NULL amount is skipped by the value-based aggregates.