SQL Practice Logo

SQLPractice Online

NULL Handling in Aggregate Functions: Concept

Module: Aggregate Functions & Grouping

NULL means "unknown" or "missing" - not zero, not empty string, but truly unknown. Aggregate functions handle NULL in specific ways that can surprise you.

**How Each Aggregate Handles NULL:**

• COUNT(*) - Counts all rows, including those with NULL values

• COUNT(column) - Counts only non-NULL values in that specific column

• SUM(column) - Adds up only non-NULL values, completely ignores NULL

• AVG(column) - Averages only non-NULL values (does NOT treat NULL as zero)

• MIN/MAX(column) - Finds smallest/largest non-NULL value, ignores NULL

**Critical Rule: When ALL Values Are NULL**

If every value in a column is NULL, the aggregate returns NULL (not zero):

- SUM of all NULL = NULL

- AVG of all NULL = NULL

- COUNT(column) of all NULL = 0

- COUNT(*) still counts the rows

Critical for data analysts, BI developers, and backend engineers. NULL mishandling is the #1 cause of incorrect reports in finance, HR, and SaaS analytics.

NULL values in aggregates are everywhere and dangerous. Missing employee bonuses make average salary calculations wrong. NULL discount codes break revenue reports. Incomplete survey responses skew analytics. Understanding NULL behavior prevents silent errors.