HAVING Function

HAVING function is used to attach conditions to any type of SQL function SUM, MAX, ..

As the WHERE clause can not use SQL functions of the type SUM, MAX, then we use instead HAVING.

HAVING syntax

SELECT column1, SUM (column2) FROM table GROUP BY column1 HAVING SUM (column2) < number


Given the following table 'orders'

id Order client price
1 p1 HARRY 100
2 p2 CLARK 300
3 p3 SMITH 250
4 p4 HARRY 490
5 p5 SMITH 60

SELECT customer, SUM (price) FROM orders GROUP BY customer HAVING SUM (price) > 500

The result will be:

client SUM (price)

Only the client 'HARRY' sum their values ??to 'price' 490 + 100 = 590 a value greater than 500

The client 'SMITH' sum the values ??for column 'price' 250 + 60 = 310, which is also less than 500, and therefore does not appear in the result table.

The client 'CLARK' gets a value of 300, and does not appear in the table, because it is less than 500.