News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

How to select the columns using Groupby clause?

Started by VelMurugan, May 23, 2009, 09:13 PM

Previous topic - Next topic

VelMurugan

Group By :

    The Group by clause is used to display the rows and columns grouped by selective columns. It can be used to perform the aggregate functions, such as count().

    The following example query will list the name of the student and also count the repeative names using Group By clause in the select statement.

mysql> select name, count(name) from student group by name;
+-------+-------------+
| name  | count(name) |
+-------+-------------+
| anne  |           2 |
| david |           1 |
| jack  |           1 |
| mille |           1 |
| steve |           2 |
+-------+-------------+
5 rows in set (0.03 sec)

    The below query will display the name and sum of marks of the student using groupby clause.

mysql> select name,sum(marks),count(*) from students group by name;
+----------+------------+----------+
| name     | sum(marks) | count(*) |
+----------+------------+----------+
| anne     |        175 |        2 |
| maichael |         82 |        1 |
| mike     |        182 |        2 |
| rock     |        100 |        1 |
| steve    |        175 |        2 |
+----------+------------+----------+
5 rows in set (0.00 sec)