SQL Group By

We saw some of the group functions, which treats the table as one large group of information. At times we may want to divide the table of information into smaller groups. This can be done by using group by clause. The syntax is :

Select column, group_function (column)
From table
[where condition]
[group by column|expr]
Order by column] ;

In the above syntax :
Column|expr specifies columns whose values determine the basis for grouping rows.
We can use the group by clause to divide the rows in a table into groups. We can then use the group function to return summary information for each group.
Note:
All columns in the select list that are not in group functions must be in the group by clause. Using a where clause we can exclude rows before dividing them into groups. We cannot use a column alias in the group by clause. By default rows are stored by ascending order of the columns included in the group by list. We can overwrite this by using the order by clause.

Example:


Select dept-id, avg(emp-salary)
From employee
Group by dept-id ;

Note:
it is not necessary that the column we are using in group by clause should exist in the select list . it also possible to specify more than one column name in the group by clause. In that case the first column will be grouped first within that the second column will be grouped.



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved