The GROUP BY clause allows you to group values according to a set of properties.
The following query groups the customers by their country and returns the number of customers per country:
SELECT c.country, COUNT(c) FROM Customer c GROUP BY c.country
The HAVING clause is used with the GROUP BY clause to further restrict the returned result of a query.
The following query groups orders by the status of their customer and returns the customer status plus the average totalPrice for all orders where the corresponding customers has the same status. In addition, it considers only customers with status 1, 2, or 3, so orders of other customers are not taken into account:
SELECT c.status, AVG(o.totalPrice) FROM Order o JOIN o.customer c GROUP BY c.status HAVING c.status IN (1, 2, 3)