Aggregate functions (COUNT, SUM, AVG, MAX, MIN) in DBMS Theory - Time & Space Complexity
Aggregate functions process multiple rows to produce a single result, like counting or summing values.
We want to know how the time to get these results changes as the number of rows grows.
Analyze the time complexity of the following SQL query using aggregate functions.
SELECT COUNT(*), SUM(salary), AVG(age), MAX(score), MIN(score)
FROM employees;
This query calculates the total number of employees, the sum of their salaries, the average age, and the highest and lowest scores.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the employees table once.
- How many times: Once per row, to update each aggregate value.
As the number of rows increases, the database reads each row once to update the aggregates.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (one per row) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles work.
Time Complexity: O(n)
This means the time to compute aggregates grows linearly with the number of rows.
[X] Wrong: "Aggregate functions run instantly regardless of data size."
[OK] Correct: Each row must be checked to update counts or sums, so more data means more work.
Understanding how aggregate functions scale helps you explain database performance clearly and confidently.
"What if the query included a GROUP BY clause? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the purpose of each aggregate function
COUNTcounts the number of rows,SUMadds values,AVGcalculates average, andMAXfinds the highest value.Step 2: Identify the function that counts rows
The function that counts rows isCOUNT.Final Answer:
COUNT -> Option AQuick Check:
Counting rows = COUNT [OK]
- Confusing SUM with COUNT
- Using AVG to count rows
- Thinking MAX counts rows
Employees?Solution
Step 1: Recall the standard SQL function for average
The standard SQL function to calculate average isAVG().Step 2: Check the syntax correctness
SELECT AVG(salary) FROM Employees;is the correct syntax. Other options use non-standard functions or expressions.Final Answer:
SELECT AVG(salary) FROM Employees; -> Option BQuick Check:
Average function = AVG() [OK]
- Using non-standard function names like AVERAGE or MEAN
- Trying to calculate average manually with SUM/COUNT
- Missing parentheses in function call
Sales with a column amount having values: 100, 200, 300, 400. What will be the result of the query SELECT MAX(amount) FROM Sales;?Solution
Step 1: Identify the values in the column
Theamountcolumn has values 100, 200, 300, and 400.Step 2: Apply the MAX function
MAX(amount)returns the highest value, which is 400.Final Answer:
400 -> Option AQuick Check:
Maximum of 100,200,300,400 = 400 [OK]
- Choosing SUM or AVG instead of MAX
- Picking the lowest value by mistake
- Adding values instead of finding max
SELECT SUM(price), COUNT FROM Products;Solution
Step 1: Analyze the use of COUNT function
TheCOUNTfunction must be called with parentheses, likeCOUNT(*)orCOUNT(column_name).Step 2: Identify the syntax error
The query usesCOUNTwithout parentheses, which is invalid syntax.Final Answer:
COUNT requires parentheses with a column or * -> Option DQuick Check:
COUNT() needs parentheses [OK]
- Writing COUNT without parentheses
- Assuming SUM can't be used on price
- Thinking WHERE clause is mandatory
Orders with columns order_id, customer_id, and amount. You want to find the average order amount for each customer. Which SQL query correctly uses aggregate functions to achieve this?Solution
Step 1: Understand the requirement
We want the average order amount per customer, so we need to group data bycustomer_idand calculate averageamount.Step 2: Check each query's correctness
SELECT customer_id, AVG(amount) FROM Orders GROUP BY customer_id; groups bycustomer_idand usesAVG(amount), which is correct. SELECT customer_id, SUM(amount) FROM Orders; sums amounts but does not group, so it gives total sum only. SELECT AVG(amount) FROM Orders WHERE customer_id; has invalid WHERE clause usage. SELECT customer_id, COUNT(amount) FROM Orders GROUP BY order_id; groups byorder_id, which is wrong for per customer aggregation.Final Answer:
SELECT customer_id, AVG(amount) FROM Orders GROUP BY customer_id; -> Option CQuick Check:
Group by customer_id + AVG(amount) [OK]
- Not using GROUP BY when aggregating per group
- Using SUM instead of AVG for average
- Incorrect WHERE clause instead of GROUP BY
Ready to prove it?
Pass the test and this pattern moves unknown
