NULL behavior in aggregate functions in SQL - Time & Space Complexity
When using aggregate functions in SQL, NULL values can affect how many rows are processed.
We want to understand how the presence of NULLs changes the work done by these functions.
Analyze the time complexity of the following SQL query.
SELECT AVG(salary) FROM employees;
This query calculates the average salary, ignoring NULL salaries.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the employees table once.
- How many times: Once per row, checking if salary is NULL or not.
The query looks at every row to find salaries that are not NULL and sums them.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and sums |
| 100 | 100 checks and sums |
| 1000 | 1000 checks and sums |
Pattern observation: The work grows directly with the number of rows, regardless of how many NULLs there are.
Time Complexity: O(n)
This means the time to compute the average grows linearly with the number of rows in the table.
[X] Wrong: "NULL values cause the query to skip rows and reduce the time needed."
[OK] Correct: The query still checks every row to see if the value is NULL, so the total work depends on all rows, not just non-NULL ones.
Understanding how NULLs affect aggregate functions helps you explain query performance clearly and shows you know how databases handle data internally.
"What if we changed AVG(salary) to COUNT(salary)? How would the time complexity change?"
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
