Notes

`GROUP BY` Clause in DBMS (MySQL) [ English ]

< Prev Next >

Introduction

In a Database Management System (DBMS), data stored in tables may contain many records. Sometimes users need to organize data into groups to perform calculations such as counting records, finding averages, or calculating totals.

The GROUP BY clause is used to group rows that have the same values in specified columns. It is often used together with aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MAX().

Definition

The GROUP BY clause is used in SQL to group rows that share the same values in specified columns and perform aggregate operations on those groups.

Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;

Where:

Common Aggregate Functions

Function Description
COUNT() Counts the number of rows
SUM() Calculates the total value
AVG() Finds the average value
MIN() Returns the smallest value
MAX() Returns the largest value

Example Table

Suppose we have a table named students.

id name age course
1 Rahul 20 BCA
2 Anita 21 BBA
3 Rohit 19 BCA
4 Priya 22 BCom
5 Arjun 20 BCA

Example 1: Counting Students in Each Course

SELECT course, COUNT(*) 
FROM students
GROUP BY course;

Output

course COUNT(*)
BCA 3
BBA 1
BCom 1

This query groups records by course and counts the number of students in each course.

Example 2: Finding Average Age by Course

SELECT course, AVG(age)
FROM students
GROUP BY course;

Output

course AVG(age)
BCA 19.67
BBA 21
BCom 22

This query calculates the average age of students in each course.

Using GROUP BY with Multiple Columns

It is possible to group data based on more than one column.

SELECT column1, column2, COUNT(*)
FROM table_name
GROUP BY column1, column2;

Example:

SELECT course, age, COUNT(*)
FROM students
GROUP BY course, age;

This groups students by both course and age.

Important Notes

Example:

SELECT course, COUNT(*)
FROM students
WHERE age > 19
GROUP BY course;

Key Points

Summary

The GROUP BY clause is used in SQL to organize data into groups based on one or more columns. It is commonly used with aggregate functions to perform calculations such as counting records, finding averages, or computing totals. This makes it a powerful tool for summarizing and analyzing data stored in a database.

< Prev Next >