Group By

SQL GROUP BY

The GROUP BY is used when aggregate functions exists in the select.
The aggregate functions are: count, max, min, sum, avg.

Syntax

SELECT name_of_column, function(name_of_column)
FROM name_of_table
GROUP BY name_of_column

Example

Orders

IDNAMEPRICE
1Learn Python45
2MySQL Tutorial32
3PHP examples24
4Learn SQL32
2MySQL Tutorial32
SELECT id, name, SUM(price) as total 
FROM orders
GROUP BY id, name
ORDER BY total desc;

Output

IDNAMETOTAL
2MySQL Tutorial64
1Learn Python45
4Learn SQL32
3PHP examples24