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.
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.
Learning SQL in order?
Use the roadmap to follow each SQL topic step by step, from relational database basics to advanced query techniques.
Back to SQL RoadmapRelated lessons
ALTER TABLE
A beginner-friendly guide to ALTER TABLE in SQL, including how to add, change, and remove columns from an existing table.
CREATE TABLE
A beginner-friendly guide to the SQL CREATE TABLE statement, including table planning, column names, data types, and a SQL Server-friendly example.
DELETE
A beginner-friendly guide to the SQL DELETE statement, with SQL Server-friendly examples for removing rows from a table safely.