SQL GROUP BY

The GROUP BY function is used to gather results matching rows in the value of any selected column.

Syntax GROUP BY

SELECT column, function (column) FROM table
GROUP BY column

Example using GROUP BY

Given the following table 'orders'

idorderclientprice
1p1HARRY100
2p2CLARK300
3p3SMITH250
4p4 HARRY 490
5p5 SMITH 60
SELECT cliente, SUM(price) FROM orders GROUP BY client

The result is as follows:

clientSUM(precio)
HARRY590
CLARK300
SMITH310

The client 'HARRY' is repeated 2 times, add up the values for column 'price' 490 + 100 = 590

The client 'CLARK' stays the same, with the value for 'price' of 300.

The client 'SMITH' is repeated 2 times, and add their values for column 'price' 250 + 60 = 310

Note: You can also group by multiple columns.

GROUP BY column1, column2