Use of GROUP BY Statement in SQL:
(1) “GROUP BY” is used to group rows having the same value in a column (grouping of records on the basis of column).
(2) “GROUP BY” is mainly used with the combination of COUNT(), MAX(), MIN().
(3) Syntax of “GROUP BY” statement is:
|SELECT Col1, Col2, …ColN|
GROUP BY Col2;
(4) Let’s take an example, Grouping on the “City” column in Vendor Table.
(5) Select the database in the SQL server and click on the “New Query” button as shown.
(6) After that new query editor open as shown.
(7) After doing the above steps, write a query in the editor, as shown.
|–// With “GROUP BY”|
Select Count(No_) AS [Customer Count],City
from dbo.[CRONUS India Ltd_$Vendor$437dbf0e-84ff-417a-965d-ed2bb9650972]
Group BY City
Order By Count(No_) Desc
–// With “GROUP BY
(8) After writing the query, then execute the query (Shortcut keys is F5) as shown.
(9) After executing the two queries in the same editor, SQL gives two results.