GROUP BY helps organize data into groups so you can summarize or count items easily.
How GROUP BY changes query execution in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SQL
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
You must list the column(s) you want to group by after GROUP BY.
Columns in SELECT that are not aggregated must be in GROUP BY.
Examples
SQL
SELECT department, COUNT(*) FROM employees GROUP BY department;
SQL
SELECT product, SUM(quantity) FROM sales GROUP BY product;
SQL
SELECT city, AVG(age) FROM customers GROUP BY city;
Sample Program
This example creates a sales table, adds some sales data, and then groups the data by product to find total quantity sold per product.
SQL
CREATE TABLE sales ( product VARCHAR(20), quantity INT ); INSERT INTO sales VALUES ('Apple', 10), ('Banana', 5), ('Apple', 7), ('Banana', 3), ('Cherry', 8); SELECT product, SUM(quantity) AS total_quantity FROM sales GROUP BY product;
Important Notes
GROUP BY changes the query to work on groups of rows, not individual rows.
Aggregate functions like SUM, COUNT, AVG work with GROUP BY to summarize data.
Without GROUP BY, aggregate functions consider the whole table as one group.
Summary
GROUP BY groups rows with the same values in specified columns.
It allows aggregate functions to calculate summaries per group.
Use GROUP BY when you want to analyze data by categories or groups.
Practice
1. What does the
GROUP BY clause do in an SQL query?easy
Solution
Step 1: Understand the purpose of GROUP BY
The GROUP BY clause collects rows with the same values in specified columns into groups.Step 2: Compare with other SQL clauses
Sorting is done by ORDER BY, filtering by WHERE, and removing duplicates by DISTINCT, not GROUP BY.Final Answer:
It groups rows that have the same values in specified columns. -> Option BQuick Check:
GROUP BY groups rows by column values [OK]
Hint: GROUP BY groups rows by column values, not sorting or filtering [OK]
Common Mistakes:
- Confusing GROUP BY with ORDER BY
- Thinking GROUP BY filters rows
- Assuming GROUP BY removes duplicates
2. Which of the following is the correct syntax to group rows by the column
department?easy
Solution
Step 1: Identify correct GROUP BY usage
The GROUP BY clause must follow the FROM clause and specify the column to group by, here 'department'.Step 2: Check each option's syntax
SELECT department, COUNT(*) FROM employees GROUP BY department; uses GROUP BY correctly. SELECT department, COUNT(*) FROM employees ORDER BY department; uses ORDER BY which sorts, not groups. SELECT department, COUNT(*) FROM employees WHERE department; uses WHERE incorrectly. SELECT department, COUNT(*) FROM employees HAVING department; uses HAVING without GROUP BY, which is invalid.Final Answer:
SELECT department, COUNT(*) FROM employees GROUP BY department; -> Option CQuick Check:
GROUP BY syntax: SELECT ... FROM ... GROUP BY column [OK]
Hint: GROUP BY follows FROM and lists columns to group by [OK]
Common Mistakes:
- Using ORDER BY instead of GROUP BY
- Using WHERE to group rows
- Using HAVING without GROUP BY
3. Given the table
sales with columns region and amount, what is the output of this query?SELECT region, SUM(amount) FROM sales GROUP BY region;
medium
Solution
Step 1: Understand GROUP BY with aggregate functions
The query groups rows by 'region' and calculates the sum of 'amount' for each group.Step 2: Analyze the output
The result shows each region once with the total sales amount summed from all rows in that region.Final Answer:
A list of regions with the total sales amount per region. -> Option AQuick Check:
GROUP BY + SUM() = totals per group [OK]
Hint: GROUP BY with SUM() gives totals per group [OK]
Common Mistakes:
- Expecting all rows without grouping
- Thinking SUM() causes error with GROUP BY
- Confusing grouping with sorting
4. What is wrong with this query?
SELECT department, employee_name, COUNT(*) FROM employees GROUP BY department;
medium
Solution
Step 1: Check columns in SELECT with GROUP BY
When using GROUP BY on 'department', all selected columns must be grouped or aggregated.Step 2: Identify the error
'employee_name' is neither grouped nor aggregated, causing a syntax error.Final Answer:
You cannot select employee_name without grouping by it or using an aggregate function. -> Option AQuick Check:
Non-grouped columns must be aggregated [OK]
Hint: All selected columns must be grouped or aggregated [OK]
Common Mistakes:
- Selecting non-grouped columns without aggregation
- Thinking COUNT(*) is invalid with GROUP BY
- Misplacing GROUP BY clause
5. You want to find departments with more than 5 employees. Which query correctly uses GROUP BY and HAVING to achieve this?
hard
Solution
Step 1: Understand HAVING clause usage
HAVING filters groups after aggregation, so it must come after GROUP BY.Step 2: Check query order and syntax
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; correctly places HAVING after GROUP BY with condition COUNT(*) > 5. Other options misuse HAVING or WHERE clauses.Final Answer:
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; -> Option DQuick Check:
HAVING filters groups after GROUP BY [OK]
Hint: Use HAVING after GROUP BY to filter groups [OK]
Common Mistakes:
- Using WHERE to filter aggregated results
- Placing HAVING before GROUP BY
- Confusing WHERE and HAVING clauses
