SQL – GROUP BY

The GROUP BY Clause in SQL is used to club similar forms of data into groups. I.e. if a Column A has same values in different rows then it will arrange these rows in a group.

  • GROUP BY returns only one result per group of data and is used with the SELECT statement

  • GROUP BY aggregates the results on the basis of selected column: COUNT, MAX, MIN, SUM, AVG, etc.

SYNTAX

63.png

Where,

  • Group BY                                        : keyword used to group identical records in a table.

  • Table_name                                    : The name of the table from where you want to fetch the data

  • Column1Name, Column2Name       : The name of the columns of the table which you want to uniquely fetch.

EXAMPLE

Let’s query PersonalInfo Table in our Demo Database and check the count of PersonId using Group by

64.png
65.png
 

Figure 20.1. Select * from PersonalInfo;

 

Figure 20.2 Group by on PersonalInfo;

PRACTICE YOURSELF 

66.png