Loading Data Driven Insights
Preparing your learning content.
Preparing your learning content.
A beginner-friendly guide to the SQL GROUP BY clause, with SQL Server-friendly examples using COUNT and SUM.
On this page
Jump to a lesson section
The GROUP BY clause is used to group rows that have the same value.
It is commonly used with aggregate functions such as COUNT, SUM, AVG, MIN, and MAX.
A GROUP BY query usually selects one grouping column and one or more aggregate calculations.
SELECT
column_name,
aggregate_function(column_name)
FROM table_name
GROUP BY column_name;
SELECT
department,
COUNT(*) AS total_employees
FROM Employees
GROUP BY department;
We will use this Employees table to understand how GROUP BY works.
The following query groups employees by department and counts how many employees are in each group.
SELECT
department,
COUNT(*) AS total_employees
FROM Employees
GROUP BY department;
You can also use GROUP BY with SUM to calculate totals for each group.
SELECT
department,
SUM(salary) AS total_salary
FROM Employees
GROUP BY department;
Use it with aggregate functions such as COUNT, SUM, AVG, MIN, and MAX to answer questions by category.
SQL Roadmap Progress
Topic 20 of 111
18% complete
Next in SQL Roadmap
Continue with the next SQL topic in the recommended roadmap order.
Continue learning →