Bird
Raised Fist0
DBMS Theoryknowledge~5 mins

Aggregate functions (COUNT, SUM, AVG, MAX, MIN) in DBMS Theory - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Aggregate functions (COUNT, SUM, AVG, MAX, MIN)
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of rows increases, the database reads each row once to update the aggregates.

Input Size (n)Approx. Operations
1010 operations (one per row)
100100 operations
10001000 operations

Pattern observation: The work grows directly with the number of rows; doubling rows doubles work.

Final Time Complexity

Time Complexity: O(n)

This means the time to compute aggregates grows linearly with the number of rows.

Common Mistake

[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.

Interview Connect

Understanding how aggregate functions scale helps you explain database performance clearly and confidently.

Self-Check

"What if the query included a GROUP BY clause? How would that affect the time complexity?"

Practice

(1/5)
1. Which aggregate function in SQL is used to find the total number of rows in a table?
easy
A. COUNT
B. SUM
C. AVG
D. MAX

Solution

  1. Step 1: Understand the purpose of each aggregate function

    COUNT counts the number of rows, SUM adds values, AVG calculates average, and MAX finds the highest value.
  2. Step 2: Identify the function that counts rows

    The function that counts rows is COUNT.
  3. Final Answer:

    COUNT -> Option A
  4. Quick Check:

    Counting rows = COUNT [OK]
Hint: COUNT counts rows, SUM adds numbers [OK]
Common Mistakes:
  • Confusing SUM with COUNT
  • Using AVG to count rows
  • Thinking MAX counts rows
2. Which of the following is the correct syntax to find the average salary from a table named Employees?
easy
A. SELECT SUM(salary) / COUNT(*) FROM Employees;
B. SELECT AVG(salary) FROM Employees;
C. SELECT MEAN(salary) FROM Employees;
D. SELECT AVERAGE(salary) FROM Employees;

Solution

  1. Step 1: Recall the standard SQL function for average

    The standard SQL function to calculate average is AVG().
  2. Step 2: Check the syntax correctness

    SELECT AVG(salary) FROM Employees; is the correct syntax. Other options use non-standard functions or expressions.
  3. Final Answer:

    SELECT AVG(salary) FROM Employees; -> Option B
  4. Quick Check:

    Average function = AVG() [OK]
Hint: Use AVG() for average, not AVERAGE or MEAN [OK]
Common Mistakes:
  • Using non-standard function names like AVERAGE or MEAN
  • Trying to calculate average manually with SUM/COUNT
  • Missing parentheses in function call
3. Consider the table Sales with a column amount having values: 100, 200, 300, 400. What will be the result of the query SELECT MAX(amount) FROM Sales;?
medium
A. 400
B. 100
C. 250
D. 1000

Solution

  1. Step 1: Identify the values in the column

    The amount column has values 100, 200, 300, and 400.
  2. Step 2: Apply the MAX function

    MAX(amount) returns the highest value, which is 400.
  3. Final Answer:

    400 -> Option A
  4. Quick Check:

    Maximum of 100,200,300,400 = 400 [OK]
Hint: MAX returns highest value in the column [OK]
Common Mistakes:
  • Choosing SUM or AVG instead of MAX
  • Picking the lowest value by mistake
  • Adding values instead of finding max
4. What is wrong with this SQL query?
SELECT SUM(price), COUNT FROM Products;
medium
A. Products table does not exist
B. SUM cannot be used with price column
C. Missing WHERE clause
D. COUNT requires parentheses with a column or *

Solution

  1. Step 1: Analyze the use of COUNT function

    The COUNT function must be called with parentheses, like COUNT(*) or COUNT(column_name).
  2. Step 2: Identify the syntax error

    The query uses COUNT without parentheses, which is invalid syntax.
  3. Final Answer:

    COUNT requires parentheses with a column or * -> Option D
  4. Quick Check:

    COUNT() needs parentheses [OK]
Hint: Always use COUNT() with parentheses [OK]
Common Mistakes:
  • Writing COUNT without parentheses
  • Assuming SUM can't be used on price
  • Thinking WHERE clause is mandatory
5. You have a table 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?
hard
A. SELECT AVG(amount) FROM Orders WHERE customer_id;
B. SELECT customer_id, SUM(amount) FROM Orders;
C. SELECT customer_id, AVG(amount) FROM Orders GROUP BY customer_id;
D. SELECT customer_id, COUNT(amount) FROM Orders GROUP BY order_id;

Solution

  1. Step 1: Understand the requirement

    We want the average order amount per customer, so we need to group data by customer_id and calculate average amount.
  2. Step 2: Check each query's correctness

    SELECT customer_id, AVG(amount) FROM Orders GROUP BY customer_id; groups by customer_id and uses AVG(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 by order_id, which is wrong for per customer aggregation.
  3. Final Answer:

    SELECT customer_id, AVG(amount) FROM Orders GROUP BY customer_id; -> Option C
  4. Quick Check:

    Group by customer_id + AVG(amount) [OK]
Hint: Use GROUP BY with AVG for per-group averages [OK]
Common Mistakes:
  • Not using GROUP BY when aggregating per group
  • Using SUM instead of AVG for average
  • Incorrect WHERE clause instead of GROUP BY
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard