SQL GROUP BY

La clausola GROUP BY viene utilizzata per raggruppare le righe in base ad una o più colonne. Per esempio:

        SELECT country, COUNT(*) AS number
            FROM customers
        GROUP BY country;
    

GROUP BY CON PIÚ COLONNE

Possiamo anche usare GROUP BY per raggruppare le righe in base a più colonne. Per esempio:

        SELECT country, state, MIN(age) AS min_age
            FROM persons
        GROUP BY country, state;
    

GROUP BY CON HAVING

Possiamo anche usare GROUP BY con la clausola HAVING per filtrare il set di risultati in base a funzioni aggregate. Per esempio:

        SELECT COUNT(customer_id), country
            FROM customers
        GROUP BY country
            HAVING COUNT(customer_id) > 1;