AVG function in SQL - Time & Space Complexity
When we use the AVG function in SQL, the database calculates the average value of a column. Understanding how long this takes helps us know how the query performs as data grows.
We want to find out how the time to get the average changes when the number of rows increases.
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: The database scans each row in the employees table once to add up the salaries.
- How many times: Once for every row in the table.
As the number of rows grows, the database must look at more salaries to add them up before dividing.
| 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. Double the rows, double the work.
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 because it's just one function call."
[OK] Correct: The AVG function must look at every row's value to add them up before dividing, so more rows mean more work.
Knowing how aggregate functions like AVG scale with data size helps you explain query performance clearly and confidently in real situations.
"What if we added a WHERE clause to filter rows before calculating AVG? How would the time complexity change?"