Why aggregation summarizes data in MySQL - Performance Analysis
When we use aggregation in SQL, we combine many rows into fewer summary results.
We want to understand how the work grows as the data size grows.
Analyze the time complexity of the following aggregation query.
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query counts how many employees are in each department by grouping rows.
Look for repeated work done by the query.
- Primary operation: Scanning each employee row once to assign it to a group.
- How many times: Once per row in the employees table.
The query looks at every employee once to count them by department.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to summarize grows in a straight line with the number of rows.
[X] Wrong: "Aggregation is instant no matter how many rows there are."
[OK] Correct: The database must still look at each row to count or summarize it, so more rows mean more work.
Understanding how aggregation scales helps you explain query performance clearly and confidently.
"What if we added a WHERE clause to filter rows before aggregation? How would the time complexity change?"
Practice
SUM and COUNT in SQL?Solution
Step 1: Understand aggregation functions
Aggregation functions like SUM and COUNT take many rows and produce one value summarizing them.Step 2: Identify the purpose of aggregation
They are used to combine data, not to delete, create tables, or sort rows.Final Answer:
To combine multiple rows into a single summary value -> Option CQuick Check:
Aggregation = summary value [OK]
- Confusing aggregation with sorting
- Thinking aggregation deletes rows
- Believing aggregation creates new tables
employees?Solution
Step 1: Recall standard SQL aggregation functions
The correct function for average is AVG, not AVERAGE or MEAN.Step 2: Check syntax correctness
SELECT AVG(salary) FROM employees; uses the correct AVG function with proper syntax. Options B and C use non-standard functions. SELECT SUM(salary) / COUNT(*) FROM employees GROUP BY salary; uses manual SUM/COUNT but GROUP BY salary computes per-salary value instead of overall average.Final Answer:
SELECT AVG(salary) FROM employees; -> Option AQuick Check:
AVG() is standard average function [OK]
- Using non-existent functions like AVERAGE or MEAN
- Trying to calculate average manually instead of using AVG()
- Missing semicolon at the end
sales with columns region and amount, what will this query return?SELECT region, SUM(amount) FROM sales GROUP BY region;
Solution
Step 1: Understand GROUP BY with SUM
The query groups rows by region and sums the amount in each group.Step 2: Interpret the result
It returns one row per region with the total sales amount for that region.Final Answer:
Total sales amount for each region -> Option AQuick Check:
GROUP BY region + SUM(amount) = total per region [OK]
- Thinking it returns all individual sales
- Confusing SUM with AVG
- Ignoring GROUP BY effect
SELECT department, COUNT(employee_id) FROM employees;
Solution
Step 1: Check aggregation with non-aggregated column
The query selects department (non-aggregated) and COUNT(employee_id) (aggregated) without GROUP BY.Step 2: Understand SQL rules for aggregation
When mixing aggregated and non-aggregated columns, GROUP BY is required on non-aggregated columns.Final Answer:
Missing GROUP BY clause for department -> Option BQuick Check:
Non-aggregated columns need GROUP BY [OK]
- Omitting GROUP BY with mixed columns
- Misusing COUNT on non-numeric columns
- Adding unnecessary WHERE clause
employees table. Which query correctly summarizes this data?Solution
Step 1: Understand the goal
We want the department with the highest average salary, so we need average per department and then pick the top one.Step 2: Analyze each option
SELECT department, MAX(AVG(salary)) FROM employees GROUP BY department; is invalid because MAX(AVG(salary)) is not allowed directly. SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1; correctly groups by department, calculates average salary, orders descending, and limits to one result. SELECT MAX(salary) FROM employees GROUP BY department; finds max salary per department, not average. SELECT department, SUM(salary) FROM employees; sums salary but does not group or find max.Final Answer:
SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1; -> Option DQuick Check:
Group + AVG + ORDER BY + LIMIT = highest average [OK]
- Trying to nest aggregation functions incorrectly
- Not ordering results to find highest value
- Missing GROUP BY when aggregating
Ready to prove it?
Pass the test and this pattern moves unknown
