mysql  group by  statement

the MySQL group by statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

the mysql group by clause is an optional part of the select statement.

the group by clause must appear after the from or where clause.

mysql group by syntax

         select   column_name, aggregate_function(expression)

                      from   table_name
                      
                      where  condition
                      
                      group by  column_name;

database table use with group by operation

below is a selection from the "users" table:

useridfirstnamelastnamemobilenocity
1sandeepkumar9549293245india
2ramkumar9549293212usa
3shyamdhaka2323234545jaipur
4ankitnehra3423567845uae
5shyamdhaka2323234545jaipur
6radhakumari2323121212jaipur
7ramkumar9549293443usa

mysql group by with aggregate functions

the aggregate functions allow you to perform calculation of a set of records and return a single value.the most common aggregate functions are sum,avg,max,min and count.

example:

              mysql>  select        firstname , count(*)
 
                from   users
                
                group by    firstname;

output :

firstnamecount(*)
sandeep1
ram2
shyam2
ankit1
radha1