×

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

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

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.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 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 use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 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 DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

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.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

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

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.