SQL GROUP BY Clause
- The GROUP BY clause groups records into summary rows.
- GROUP BY returns one records for each group.
- GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc.
- GROUP BY can group by one or more columns.
The SQL GROUP BY syntax
The general syntax is:
- SELECT column-name FROM table-name WHERE condition GROUP BY column-names
The general syntax with ORDER BY is:
- SELECT column-names FROM table-name WHERE condition GROUP BY column-name ORDER BY column-names
SQL GROUP BY Examples
1) Example of Group By Clause
mysql> select name from person where age>20 group by name;
+-----------------+
| name |
+-----------------+
| chayan das |
| monojit |
| sajal |
| shantanu biswas |
| sourav biswas |
| subham ball |
| sudip saha |
+-----------------+
7 rows in set (0.00 sec)
+-----------------+
| name |
+-----------------+
| chayan das |
| monojit |
| sajal |
| shantanu biswas |
| sourav biswas |
| subham ball |
| sudip saha |
+-----------------+
7 rows in set (0.00 sec)
2) Example of Group By Clause with ORDER BY
mysql> select name from person where age>20 group by name order by name;
+-----------------+
| name |
+-----------------+
| chayan das |
| monojit |
| sajal |
| shantanu biswas |
| sourav biswas |
| subham ball |
| sudip saha |
+-----------------+
7 rows in set (0.00 sec)
+-----------------+
| name |
+-----------------+
| chayan das |
| monojit |
| sajal |
| shantanu biswas |
| sourav biswas |
| subham ball |
| sudip saha |
+-----------------+
7 rows in set (0.00 sec)
Comments
Post a Comment