Why aggregation is needed in SQL - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run aggregation queries changes as data grows.
How does grouping and summarizing data affect the work the database does?
Analyze the time complexity of the following code snippet.
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query counts how many employees are in each department by grouping rows.
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 assign it to a group and update the count.
As the number of employees grows, the database must look at each employee once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (one per employee) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the aggregation grows in a straight line with the number of rows.
[X] Wrong: "Aggregation queries are always slow because they do extra work."
[OK] Correct: Aggregation just looks at each row once, so it grows linearly, not slower or faster than scanning the data.
Understanding how aggregation scales helps you explain query performance clearly and confidently.
"What if we added an ORDER BY after the GROUP BY? How would the time complexity change?"
Practice
SUM() or COUNT() in SQL?Solution
Step 1: Understand aggregation functions
Aggregation functions like SUM and COUNT combine many rows into one summary value.Step 2: Identify the purpose of aggregation
They help get totals, counts, or averages instead of listing every row.Final Answer:
To summarize multiple rows into a single value -> Option AQuick Check:
Aggregation = summarize rows [OK]
- Thinking aggregation deletes data
- Confusing aggregation with data type changes
- Assuming aggregation creates new tables
Orders with a column Amount?Solution
Step 1: Identify the correct aggregation function for total
The function to add values is SUM(), so SUM(Amount) is correct.Step 2: Check syntax correctness
SUM(Amount) with SELECT and FROM table is valid SQL syntax.Final Answer:
SELECT SUM(Amount) FROM Orders; -> Option CQuick Check:
SUM() sums values [OK]
- Using TOTAL() which is not standard SQL
- Using COUNT() instead of SUM() for totals
- Using ADD() which is not a SQL function
Sales with columns Region and Amount, what will this query return?SELECT Region, COUNT(*) FROM Sales GROUP BY Region;
Solution
Step 1: Understand COUNT(*) with GROUP BY
COUNT(*) counts rows in each group defined by Region.Step 2: Interpret the query result
The query returns how many sales records exist for each Region.Final Answer:
The number of sales records per region -> Option AQuick Check:
COUNT(*) with GROUP BY = count rows per group [OK]
- Thinking COUNT(*) sums amounts
- Confusing COUNT(*) with AVG()
- Ignoring GROUP BY effect
SELECT Department, AVG(Salary) FROM Employees;
Solution
Step 1: Check aggregation with multiple columns
When selecting Department and AVG(Salary), Department must be grouped.Step 2: Identify missing GROUP BY
The query lacks GROUP BY Department, causing error or wrong results.Final Answer:
Missing GROUP BY clause for Department -> Option BQuick Check:
Aggregation with columns needs GROUP BY [OK]
- Forgetting GROUP BY with aggregation
- Thinking AVG() can't be used on numbers
- Assuming SELECT can have unrelated columns
Sales table with columns Department and Amount. Which query correctly achieves this?Solution
Step 1: Aggregate total sales per department
SUM(Amount) with GROUP BY Department calculates total sales per department.Step 2: Order and limit to get highest total
ORDER BY SUM(Amount) DESC sorts totals from highest to lowest, LIMIT 1 picks top department.Final Answer:
SELECT Department, SUM(Amount) FROM Sales GROUP BY Department ORDER BY SUM(Amount) DESC LIMIT 1; -> Option DQuick Check:
Group, sum, order desc, limit 1 = top total [OK]
- Using MAX(Amount) instead of SUM(Amount)
- Not grouping by Department
- Trying to filter with WHERE and aggregation
