GROUP BY single column in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use GROUP BY on one column, the database groups rows by that column's values.
We want to know how the work grows as the number of rows increases.
Analyze the time complexity of the following code snippet.
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
This query counts how many employees are in each department by grouping rows by the department column.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning all rows once to group by department.
- How many times: Once for each row in the employees table.
As the number of rows grows, the database must look at each row once to group it.
| 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 group grows in a straight line as the number of rows increases.
[X] Wrong: "Grouping by one column means the database only looks at unique values, so it's very fast regardless of rows."
[OK] Correct: The database must still check every row to know which group it belongs to, so the total work depends on the number of rows, not just unique groups.
Understanding how grouping scales helps you explain query performance clearly and confidently in real situations.
"What if we added an index on the department column? How would the time complexity change?"
Practice
GROUP BY clause do in an SQL query?Solution
Step 1: Understand the purpose of GROUP BY
The GROUP BY clause collects rows with the same value in the specified column into groups.Step 2: Differentiate from other clauses
Unlike ORDER BY which sorts, or WHERE which filters, GROUP BY organizes data for aggregation.Final Answer:
It groups rows that have the same values in a specified column. -> Option CQuick Check:
GROUP BY = grouping rows by column [OK]
- Confusing GROUP BY with ORDER BY
- Thinking GROUP BY filters rows
- Assuming GROUP BY deletes duplicates
department?Solution
Step 1: Identify correct GROUP BY usage
The GROUP BY clause must follow FROM and group by the column named.Step 2: Check each option's clause
SELECT department, COUNT(*) FROM employees GROUP BY department; uses GROUP BY correctly; others use ORDER BY, WHERE, HAVING incorrectly here.Final Answer:
SELECT department, COUNT(*) FROM employees GROUP BY department; -> Option AQuick Check:
GROUP BY syntax = SELECT ... GROUP BY column [OK]
- Using ORDER BY instead of GROUP BY
- Using WHERE to group data
- Using HAVING without aggregation
sales with columns region and amount, what is the output of this query?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 for each group.Step 2: Predict output format
The output shows each region once with the total amount of sales in that region.Final Answer:
A list of regions with the total sales amount for each region. -> Option BQuick Check:
GROUP BY + SUM() = grouped sums [OK]
- Thinking SUM() can't be used with GROUP BY
- Expecting ungrouped list
- Confusing sorting with grouping
SELECT department, COUNT(*) FROM employees;
Solution
Step 1: Check SELECT with aggregation
COUNT(*) is an aggregate but department is not aggregated or grouped.Step 2: Identify missing GROUP BY
To select department with COUNT(*), GROUP BY department is required.Final Answer:
Missing GROUP BY clause for the department column. -> Option DQuick Check:
Non-aggregated columns need GROUP BY [OK]
- Ignoring missing GROUP BY
- Thinking COUNT(*) needs WHERE
- Assuming query runs without error
orders with columns customer_id, order_date, and total. You want to find the average order total per customer but only for customers who have placed more than 3 orders. Which query correctly achieves this?Solution
Step 1: Use GROUP BY to group orders by customer_id
This groups all orders per customer to calculate aggregates.Step 2: Use HAVING to filter groups with more than 3 orders
HAVING filters groups after aggregation; WHERE cannot filter aggregates.Final Answer:
SELECT customer_id, AVG(total) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; -> Option AQuick Check:
HAVING filters groups, WHERE filters rows [OK]
- Using WHERE to filter aggregated counts
- Placing HAVING before GROUP BY
- Not filtering groups at all
