×

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 Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 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.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.