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().
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.
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;
Where:
| 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 |
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 |
SELECT course, COUNT(*)
FROM students
GROUP BY course;
| course | COUNT(*) |
|---|---|
| BCA | 3 |
| BBA | 1 |
| BCom | 1 |
This query groups records by course and counts the number of students in each course.
SELECT course, AVG(age)
FROM students
GROUP BY course;
| course | AVG(age) |
|---|---|
| BCA | 19.67 |
| BBA | 21 |
| BCom | 22 |
This query calculates the average age of students in each course.
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.
GROUP BY is used with aggregate functions.
All columns in the SELECT list must either be:
GROUP BY clause, orThe GROUP BY clause usually appears after the WHERE clause and before ORDER BY.
Example:
SELECT course, COUNT(*)
FROM students
WHERE age > 19
GROUP BY course;
GROUP BY is used to group rows with the same values.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.