GROUP BY Clause in SQL

11/22/2025
All Articles

SQL GROUP BY tutorial with examples and explanations

GROUP BY Clause in SQL

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


πŸ”Ή 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

id name department salary
1 Amit IT 50000
2 Neha HR 45000
3 Rahul IT 60000
4 Sara HR 55000
5 Karan Finance 70000

πŸ”Έ Example 1: Count employees per department

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

βœ” Output

department total_employees
IT 2
HR 2
Finance 1

πŸ”Έ 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

Feature WHERE HAVING
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.

Article