English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQL GROUP BY Keyword Usage and Example

SQL Keyword Reference

GROUP BY

The GROUP BY command is used to group the result set (used with aggregate functions, such as: COUNT, MAX, MIN, SUM, AVG).

The following SQL lists each country/Number of customers by region:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

The following SQL lists each country/Number of customers by region, sorted from high to low:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

SQL Keyword Reference