GROUP BY Clause in SQL

11/22/2025

SQL GROUP BY tutorial with examples and explanations

Go Back

GROUP BY Clause in SQL – SQL Tutorial for Beginners

The GROUP BY clause in SQL is used to group rows that have the same values in one or more columns. It is commonly used with aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MAX().

In this beginner-friendly SQL tutorial, you will learn:

  • What GROUP BY is

  • How GROUP BY works

  • Syntax and examples

  • GROUP BY with aggregate functions

  • GROUP BY with multiple columns

  • HAVING vs WHERE

  • Real-world use cases

  • Best practices


SQL GROUP BY tutorial with examples and explanations

🔹 What Is GROUP BY?

The GROUP BY clause groups rows that have the same value in a column into summary rows.

GROUP BY is mainly used to:

  • Generate summarized reports

  • Count records by category

  • Calculate totals and averages

  • Categorize data by groups

Example outputs include:

  • Number of employees per department

  • Total sales per product

  • Average salary per city


🔸 GROUP BY Syntax

SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;

🔹 Example Table: employees

idnamedepartmentsalary
1AmitIT50000
2NehaHR45000
3RahulIT60000
4SaraHR55000
5KaranFinance70000

🔸 Example 1: Count employees per department

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;

✔ Output

departmenttotal_employees
IT2
HR2
Finance1

🔸 Example 2: Total salary by department

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

🔸 Example 3: Average salary by department

SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

🔹 GROUP BY with Multiple Columns

You can group by more than one column.

Example:

SELECT department, city, COUNT(*) AS total
FROM employees
GROUP BY department, city;

🔹 Using GROUP BY with HAVING Clause

The HAVING clause filters grouped results.

Example: Departments with total salary above 1,00,000

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
HAVING total_salary > 100000;

🔹 WHERE vs HAVING

FeatureWHEREHAVING
Filters rows before grouping
Filters rows after grouping
Can use aggregate functions

Example:

Filter IT department before grouping:

SELECT department, COUNT(*)
FROM employees
WHERE department = 'IT'
GROUP BY department;

🔹 Real-World Examples

✔ Total orders per customer

SELECT customer_id, COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id;

✔ Total sales per product

SELECT product_name, SUM(quantity) AS total_sold
FROM sales
GROUP BY product_name;

✔ Active users per city

SELECT city, COUNT(*)
FROM users
GROUP BY city;

🔹 Best Practices for GROUP BY

✔ Use meaningful aliases (AS total_salary, AS count)
✔ Always pair GROUP BY with an aggregate function
✔ Ensure grouped columns exist in SELECT
✔ Use HAVING for filtered aggregated results
✔ Index grouping columns for performance


Summary

In this SQL GROUP BY tutorial, you learned:

  • How GROUP BY groups rows with common values

  • How to use aggregate functions (COUNT, SUM, AVG)

  • How HAVING filters grouped results

  • Real-world examples and best practices

The GROUP BY clause is essential for reporting, analytics, and summarizing large datasets.