Loading, please wait...

Group By Clause

How To Use Group By Clause In SQL

 

Introduction:
In this article, you will learn, what group by clause is and how to use group by clause in SQL.

This clause is used to arrange the data into the groups. The group by clause depends on the where clause and must introduce the order by clause if one requires it.

 

Syntax

The basic syntax of the group by clause is as follows:

Select column1, column2... from table_name where [conditions] Group By column1, column2.. ;

 

Example

Step 1: In this query, we find only the name of the person and his salary. We use this group by clause if we want to know the total amount of salary for each customer.

 

Thus, the query is:

 

 

 

Now, we select the query and execute it.

 

Step 2: The output of this query will be:

 

 

 

Step 3: Now, we have the records, given below, in the employee table with the duplicate names.

 

 

 

If we want to calculate the total amount of salary, we remove the duplicate names.

 

Step 4: Thus, we use group by clause to remove the duplicate names.

 

 

 

Step 5: The result is:

 

 

 

Summary:
Thus, we learned, how does group by clause work in SQL.