Grouping data with GROUP BY

•    The GROUP BY statement groups rows that have the same values into summary rows, separating them into groups based on the values in the grouping columns.
•    The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result set by one or more columns.

 

SELECT column_name(s)

FROM table_name

WHERE condition

GROUP BY column_name(s)

GROUP BY column_name(s);
 

EXAMPLE:

 

SELECT column_name(s)

FROM SALES

GROUP BY Salesperson;

 

SQL statement lists the number of customers in each country, sorted from high to low:

 

SELECT COUNT (CustomerID), Country

FROM Customers

GROUP BY Country

ORDER BY COUNT (CustomerID) DESC;