×

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is equivalent to each other.

We can use the GROUP BY clause with the SELECT query in the Structured Query Language. We placed the GROUP BY clause after the WHERE clause and before the ORDER BY clause in the SQL queries.

The syntax for the SQL GROUP BY clause is:

SELECT  * FROM Table_Name GROUP BY Column_Name;

The above syntax will select all the data or records from the table name mentioned, and it will arrange all those data or records in the group by column name.

The syntax for SQL GROUP BY clause with a specific column is given below:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name__4, Column_Name__5 FROM Table_Name GROUP BY Column_Name;

The above syntax will display records of selective columns only and arrange those column data in the group.

The GROUP BY clause specially used with the aggregate function i.e. max(), min(), avg(), sum(), count() to group result by one or more than one columns.

The below syntax is used by the SQL GROUP BY clause using Aggregate Functions is as follows:

SELECT Column_Name_1, Function_Name(Column_Name) FROM Table_Name GROUP BY Column_Name;

Let’s understand the SQL GROUP BY Clause with the help of an example.

Consider the already existing table with the following data:

Table Name: Employee_Details

E_IdE_NameE_SalaryE_CityDesignationAge
1001Kirti Kirtane60000MumbaiProject Manager26
1002Akash Vani40000PuneSystem Engineer22
1003Anupam Mishra55000HyderabadProject Manager25
1004Anuj Rawat45000HyderabadSoftware Tester25
1005Akanksha Yadav42000PuneAssociate Software Developer23
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
2001Sakshi Sharma40000BangaloreSystem Engineer23
2002Megha Ghatole45000BangaloreSoftware Tester24
2003Surbhi Nahar60000PuneProject Manager26
2004Supriya Shende55000MumbaiSoftware Developer25
2005Prachi Sharma52000HyderabadSoftware Developer24
2006Purva Dhandekar50000BangaloreSoftware Tester23
3001Shruti Deshpande60000PuneProject Manager26
3002Rohit Nikam40000HyderabadSystem Engineer23
3003Sahil Jain50000MumbaiSoftware Developer24

Example 1: Write a query to display employee’s details group by age column

SELECT * FROM Employee_Details GROUP BY Age;

The above query will display all the records of the Employee_Details table group by age column.

The output of the above query is as follows:

E_IdE_NameE_SalaryE_CityDesignationAge
1002Akash Vani40000PuneSystem Engineer22
1005Akanksha Yadav42000PuneAssociate Software Developer23
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
1003Anupam Mishra55000HyderabadProject Manager25
1001Kirti Kirtane60000MumbaiProject Manager26
SQL GROUP BY CLAUSE

Example 2: Write a query to display employee’s details group by Salary, and Designation column

SELECT * FROM Employee_Details GROUP BY E_Salary, Designation;

The above query will display all the records of the Employee_Details table group by Salary and Designation column.

The output of the above query is as follows:

E_IdE_NameE_SalaryE_CityDesignationAge
1002Akash Vani40000PuneSystem Engineer22
1005Akanksha Yadav42000PuneAssociate Software Developer23
1004Anuj Rawat45000HyderabadSoftware Tester25
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
3003Sahil Jain50000MumbaiSoftware Developer24
2006Purva Dhandekar50000BangaloreSoftware Tester23
2005Prachi Sharma52000HyderabadSoftware Developer24
1003Anupam Mishra55000HyderabadProject Manager25
2004Supriya Shende55000MumbaiSoftware Developer25
1001Kirti Kirtane60000MumbaiProject Manager26
SQL GROUP BY CLAUSE

Example 3: Write a query to display employee’s details group by Salary column in the ascending order by Employee name.

SELECT * FROM Employee_Details GROUP BY E_Salary ORDER BY E_Name;

The above query will display all the records of the Employee_Details table group by Salary column in the ascending order by employee name.

The output of the above query is:

E_IdE_NameE_SalaryE_CityDesignationAge
1005Akanksha Yadav42000PuneAssociate Software Developer23
1002Akash Vani40000PuneSystem Engineer22
1004Anuj Rawat45000HyderabadSoftware Tester25
1003Anupam Mishra55000HyderabadProject Manager25
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
1001Kirti Kirtane60000MumbaiProject Manager26
2005Prachi Sharma52000HyderabadSoftware Developer24
SQL GROUP BY CLAUSE

Now, let’s understand the GROUP BY clause using the aggregate function with the help of an example

Consider the already existing table with the following data:

Table Name:  Diploma_Students

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881

Example 1: Write a query to display the total number of student percentages scored in the First-Semester, and Second-Semester column GROUP BY First-Semester, and Second-Semester column

SELECT Count(Student_Name)  AS 'Total Number', First_Sem, Second_Sem FROM Diploma_Student GROUP BY First_Sem, Second_Sem;

The above query displays how many students scored percentages in the first-semester, second-semester GROUP BY first-semester, and second-semester.

The output of the above query is as follows:

Total NumberFirst_SemSecond_Sem
17068
17075
17580
17875
18075
18078
18080
18085
18088
18575
38580
18588
18590
19080
19088
19089
19491
19590
SQL GROUP BY CLAUSE

Example 2: Write a query to sum the student total column percentage group by Department id.

SELECT SUM(Total) AS 'Total', Department_Id FROM Diploma_Student GROUP BY Department_Id;

We summed the student total column group by department id in the above query.

The output of the above query is as follows:

TotalDepartment_Id
2481
3202
2573
2544
1615
1776
2477
SQL GROUP BY CLAUSE

Example 3: Write a query to display the maximum total column percentage of the student GROUP BY Department id.

SELECT MAX(Total) as 'Maximum Percentage', Department_Id FROM Diploma_Student GROUP BY Department_Id;

In the above query, we displayed the maximum student percentage of the total column GROUP BY Department Id

The output of the above query is as follows:

Maximum PercentageDepartment_Id
881
862
903
914
845
916
857
SQL GROUP BY CLAUSE

Example 4: Write a query to display the minimum first-semester column percentage of the student GROUP BY Department id.

SELECT MIN(First_Sem) as 'Minimum Percentage', Department_Id FROM Diploma_Student GROUP BY Department_Id;

In the above query, we displayed the minimum student percentage of the first-semester column GROUP BY Department Id

The output of the above query is as follows:

Minimum PercentageDepartment_Id
701
702
783
804
755
906
807
SQL GROUP BY CLAUSE

Related Topics

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.