What if missing data could silently ruin your totals without you noticing?
Why NULL behavior in aggregate functions in SQL? - Purpose & Use Cases
Imagine you have a big list of sales numbers, but some entries are missing or unknown (NULL). You try to add them up or find the average by hand, but you get confused about what to do with those missing numbers.
Manually ignoring or guessing what to do with missing values is slow and can cause mistakes. You might accidentally count missing data as zero or forget to exclude it, leading to wrong totals or averages.
SQL aggregate functions like SUM, AVG, COUNT, MIN, and MAX automatically handle NULL values correctly. They skip NULLs when calculating results, so you get accurate answers without extra work.
total = 0 count = 0 for value in sales: if value is not None: total += value count += 1 average = total / count
SELECT SUM(sales), AVG(sales) FROM sales_table;
This lets you quickly and reliably summarize data even when some values are missing, saving time and avoiding errors.
A store owner wants to find the average daily sales, but some days have no recorded sales (NULL). Using SQL aggregate functions, they get the correct average without manually filtering out missing days.
NULL values represent missing or unknown data.
Aggregate functions automatically ignore NULLs in calculations.
This ensures accurate summaries without extra manual filtering.
Practice
Solution
Step 1: Understand COUNT(*) behavior
COUNT(*) counts every row in the table regardless of NULLs.Step 2: Compare with other aggregate functions
COUNT(column_name) counts only non-NULL values; SUM and AVG ignore NULLs but do not count rows.Final Answer:
COUNT(*) -> Option AQuick Check:
COUNT(*) counts all rows including NULLs [OK]
- Thinking COUNT(column) counts NULLs
- Assuming SUM counts NULLs as zero
- Confusing AVG with COUNT(*)
salary?Solution
Step 1: Understand COUNT(column) syntax
COUNT(column) counts only non-NULL values in that column automatically.Step 2: Analyze other options
SELECT COUNT(*) FROM employees; counts all rows including those with NULL salary; SELECT COUNT(salary) FROM employees WHERE salary IS NULL; counts non-NULL salaries where salary is NULL (returns 0); SELECT COUNT(*) FROM employees WHERE salary IS NULL; counts rows with NULL salary.Final Answer:
SELECT COUNT(salary) FROM employees; -> Option AQuick Check:
COUNT(column) counts non-NULL values [OK]
- Using WHERE clause unnecessarily with COUNT(column)
- Confusing COUNT(*) with COUNT(column)
- Counting NULL values by mistake
sales with column amount containing values (100, NULL, 200, NULL), what is the result of SELECT SUM(amount) FROM sales;?Solution
Step 1: Identify values in amount column
Values are 100, NULL, 200, NULL.Step 2: Understand SUM behavior with NULLs
SUM ignores NULLs and adds only 100 + 200 = 300.Final Answer:
300 -> Option DQuick Check:
SUM ignores NULLs and sums non-NULLs [OK]
- Assuming SUM returns NULL if any NULL present
- Confusing SUM with COUNT
- Expecting 0 instead of sum of non-NULLs
SELECT AVG(score) FROM tests; where all score values are NULL. What is the issue and how to fix it?Solution
Step 1: Understand AVG behavior with all NULLs
AVG returns NULL if no non-NULL values exist.Step 2: Fix by filtering NULLs
Use WHERE score IS NOT NULL to exclude NULLs before averaging.Final Answer:
AVG returns NULL; fix by filtering NULLs with WHERE score IS NOT NULL -> Option BQuick Check:
AVG returns NULL if no non-NULL values [OK]
- Expecting AVG to return 0 with all NULLs
- Trying to fix with GROUP BY unnecessarily
- Using COUNT instead of AVG to get average
Solution
Step 1: Understand AVG with all NULL salaries
AVG returns NULL if no non-NULL salaries exist.Step 2: Use COALESCE to replace NULL with 0
COALESCE(AVG(salary), 0) returns 0 instead of NULL when AVG is NULL.Step 3: Analyze other options
SELECT AVG(COALESCE(salary, 0)) FROM employees; averages zeros which skews result; SELECT SUM(salary)/COUNT(*) FROM employees; divides by total rows including NULLs; SELECT COUNT(salary) FROM employees WHERE salary IS NOT NULL; counts non-NULL salaries only.Final Answer:
SELECT COALESCE(AVG(salary), 0) FROM employees; -> Option CQuick Check:
Use COALESCE to handle NULL AVG results [OK]
- Replacing NULL salaries with zero before AVG skews average
- Dividing SUM by total rows including NULLs
- Using COUNT instead of AVG for average
Ready to prove it?
Pass the test and this pattern moves unknown
