GROUP BY Clause
Collapse thousands of rows into meaningful summaries with grouping and aggregation.
- Grouping â bucket rows by one or more columns
- Aggregation â compute COUNT, SUM, AVG, MIN, MAX per group
- Filtering Groups â use HAVING to filter after aggregation Turn detail into insight.
What Is GROUP BY?
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
Syntax
SELECT
column1,
aggregate_function(column2)
FROM table_name
GROUP BY column1;
| Element | Purpose |
|---|---|
GROUP BY | Defines the grouping column(s) |
SELECT | Columns to display â must be aggregated or in GROUP BY |
| Aggregate function | COUNT, SUM, AVG, MIN, MAX, etc. |
Basic Example
-- Count employees and average salary per department
SELECT
department,
COUNT(*) AS employee_count,
ROUND(AVG(salary), 2) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC;
Single Column Grouping
-- Total salary expenditure per department
SELECT
department,
COUNT(*) AS headcount,
SUM(salary) AS total_salary,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM employees
GROUP BY department;
| Department | Headcount | Total Salary | Min | Max |
|---|---|---|---|---|
| Engineering | 45 | 60,000 | $150,000 | |
| Marketing | 20 | 45,000 | $110,000 | |
| Sales | 35 | 40,000 | $120,000 |
Multiple Column Grouping
-- Breakdown by department and job title
SELECT
department,
job_title,
COUNT(*) AS employee_count,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department, job_title
ORDER BY department, avg_salary DESC;
-- Monthly revenue by product
SELECT
YEAR(order_date) AS order_year,
MONTH(order_date) AS order_month,
product_id,
COUNT(*) AS order_count,
SUM(total_amount) AS total_revenue
FROM orders
GROUP BY YEAR(order_date), MONTH(order_date), product_id
ORDER BY order_year, order_month, total_revenue DESC;
WHERE vs HAVING
| Clause | Filters | Timing | Can Use Aggregates |
|---|---|---|---|
WHERE | Individual rows | Before GROUP BY | No |
HAVING | Groups | After GROUP BY | Yes |
Query Execution Order
-- WHERE filters rows first, then GROUP BY groups, then HAVING filters groups
SELECT
department,
COUNT(*) AS emp_count,
AVG(salary) AS avg_salary
FROM employees
WHERE status = 'Active' -- Step 1: filter rows
GROUP BY department -- Step 2: group remaining rows
HAVING AVG(salary) > 75000 -- Step 3: filter groups
ORDER BY avg_salary DESC; -- Step 4: sort
Examples
-- WHERE: filter before grouping (only active employees)
SELECT department, COUNT(*) AS emp_count
FROM employees
WHERE status = 'Active'
GROUP BY department;
-- HAVING: filter after grouping (departments with more than 5 people)
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
-- Combined: active employees, grouped, then filter high-paying departments
SELECT
department,
COUNT(*) AS emp_count,
AVG(salary) AS avg_salary
FROM employees
WHERE status = 'Active'
GROUP BY department
HAVING AVG(salary) > 75000
ORDER BY avg_salary DESC;
GROUP BY with NULLs
NULL values form their own group in GROUP BY.
-- NULLs are grouped together
SELECT
department_id,
COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
ORDER BY department_id;
-- Replace NULLs with a default value for cleaner output
SELECT
COALESCE(department_id, 0) AS department_id,
COUNT(*) AS employee_count
FROM employees
GROUP BY COALESCE(department_id, 0);
GROUP BY with ROLLUP and CUBE
-- ROLLUP: adds subtotals and a grand total
SELECT
department,
job_title,
COUNT(*) AS employee_count
FROM employees
GROUP BY ROLLUP (department, job_title);
-- CUBE: all possible subtotals (not supported in MySQL)
SELECT
department,
job_title,
COUNT(*) AS employee_count
FROM employees
GROUP BY CUBE (department, job_title);
| Extension | Subtotals Generated |
|---|---|
GROUP BY | Individual groups only |
ROLLUP | Hierarchical subtotals + grand total |
CUBE | All possible subtotal combinations + grand total |
GROUP BY with JOINs
-- Aggregate order data joined with customer info
SELECT
c.first_name,
c.last_name,
COUNT(o.order_id) AS order_count,
SUM(o.total_amount) AS total_spent
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
HAVING COUNT(o.order_id) > 5
ORDER BY total_spent DESC;
Performance Considerations
| Factor | Recommendation |
|---|---|
| Indexes | Create indexes on GROUP BY columns |
| Sorting | GROUP BY may require implicit sorting |
| Memory | Large groups can exceed memory limits |
| Statistics | Keep table statistics current |
| Execution plan | Use EXPLAIN to verify index usage |
-- Index recommendations for common GROUP BY patterns
CREATE INDEX idx_employees_department ON employees(department);
CREATE INDEX idx_employees_dept_job ON employees(department, job_title);
CREATE INDEX idx_orders_date ON orders(order_date);
CREATE INDEX idx_orders_product_date ON orders(product_id, order_date);
Practice Exercises
Exercise 1: Count the number of employees in each department.
-- Solution
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
ORDER BY employee_count DESC;
Exercise 2: Find the average salary per department for departments with more than 10 employees.
-- Solution
SELECT
department,
COUNT(*) AS employee_count,
ROUND(AVG(salary), 2) AS avg_salary
FROM employees
GROUP BY department
HAVING COUNT(*) > 10
ORDER BY avg_salary DESC;
Exercise 3: Generate a monthly sales report for 2024 with order count, total revenue, and average order value.
-- Solution
SELECT
MONTH(order_date) AS month_number,
MONTHNAME(order_date) AS month_name,
COUNT(*) AS order_count,
SUM(total_amount) AS total_revenue,
ROUND(AVG(total_amount), 2) AS avg_order_value
FROM orders
WHERE YEAR(order_date) = 2024
GROUP BY MONTH(order_date), MONTHNAME(order_date)
ORDER BY month_number;
See Also
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions