AVG function in MySQL - Time & Space Complexity
We want to understand how the time to calculate an average changes as the data grows.
How does the AVG function's work time grow when there are more rows?
Analyze the time complexity of the following code snippet.
SELECT AVG(salary) FROM employees;
This query calculates the average salary from all rows in the employees table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row to add the salary value.
- How many times: Once for every row in the employees table.
As the number of rows grows, the work to add all salaries grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to calculate the average grows in a straight line with the number of rows.
[X] Wrong: "AVG is instant no matter how many rows there are."
[OK] Correct: The database must look at every row to add values before dividing, so more rows mean more work.
Knowing how aggregate functions like AVG scale helps you explain query performance clearly and confidently.
"What if we added a WHERE clause to limit rows? How would the time complexity change?"
Practice
AVG() function do in MySQL?Solution
Step 1: Understand the purpose of AVG()
TheAVG()function is designed to calculate the average (mean) of numeric values in a column.Step 2: Compare with other aggregate functions
UnlikeCOUNT(),MAX(), orSUM(),AVG()specifically returns the average value.Final Answer:
Calculates the average value of a numeric column -> Option DQuick Check:
AVG() = average [OK]
- Confusing AVG() with COUNT()
- Thinking AVG() sums values instead of averaging
- Assuming AVG() includes NULL values in calculation
employees?Solution
Step 1: Recall correct AVG() syntax
The correct syntax usesAVG()as a function with the column name inside parentheses.Step 2: Check each option
SELECT AVG(salary) FROM employees; usesAVG(salary)correctly. Options A, C, and D have syntax errors or wrong function names.Final Answer:
SELECT AVG(salary) FROM employees; -> Option AQuick Check:
AVG(column) syntax [OK]
- Omitting parentheses in AVG()
- Using wrong function name like AVERAGE()
- Placing AVG() after column name incorrectly
scores with values:id | score
1 | 80
2 | 90
3 | NULL
4 | 70What will the query
SELECT AVG(score) FROM scores; return?Solution
Step 1: Identify values considered by AVG()
AVG() ignores NULL values, so it averages 80, 90, and 70.Step 2: Calculate average of non-NULL scores
(80 + 90 + 70) / 3 = 240 / 3 = 80.0. MySQL's AVG() returns a floating-point value.Final Answer:
80.0 -> Option CQuick Check:
AVG ignores NULL, average = 80.0 [OK]
- Including NULL in average calculation
- Expecting integer instead of float result
- Confusing AVG with SUM or COUNT
SELECT AVG(price) FROM products WHERE price > 0;It returns NULL even though there are products with positive prices. What is the likely cause?
Solution
Step 1: Understand AVG() behavior with WHERE clause
The WHERE clause filters rows before AVG() calculates the average.Step 2: Analyze why AVG() returns NULL
If the WHERE clause excludes all rows (no price > 0), AVG() has no values to average and returns NULL.Final Answer:
The WHERE clause filters out all rows -> Option BQuick Check:
Empty filtered rows cause AVG() to return NULL [OK]
- Assuming AVG() ignores WHERE clause
- Thinking AVG() returns 0 if no rows
- Believing AVG() cannot be combined with WHERE
sales table with columns region and amount. How do you write a query to find the average sales amount per region, excluding regions with no sales?Solution
Step 1: Group sales by region
UseGROUP BY regionto calculate averages per region.Step 2: Filter out regions with no sales
UseHAVING AVG(amount) > 0to exclude groups where average is NULL or 0 (no sales).Step 3: Check other options
SELECT region, AVG(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region; (SELECT region, AVG(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region;) filters NULL rows before grouping but includes regions where non-NULL amounts average to 0. Options C and D have syntax errors or misuse WHERE with GROUP BY.Final Answer:
SELECT region, AVG(amount) FROM sales GROUP BY region HAVING AVG(amount) > 0; -> Option AQuick Check:
Use HAVING to filter groups by AVG() [OK]
- Using WHERE instead of HAVING to filter groups
- Not excluding NULL averages
- Misplacing WHERE clause after GROUP BY
Ready to prove it?
Pass the test and this pattern moves unknown
