GROUP BY命令用于對結(jié)果集進(jìn)行分組(與聚合函數(shù)一起使用,如: COUNT,MAX,MIN,SUM,AVG)。
以下SQL列出了每個(gè)國家/地區(qū)的客戶數(shù)量:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
以下SQL列出了每個(gè)國家/地區(qū)的客戶數(shù)量,從高到低排序:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;