Group by (SQL)
Appearance
![]() | It has been suggested that this article be merged into Data Manipulation Language. (Discuss) Proposed since May 2007. |
A GROUP BY
statement in SQL specifies that a SQL SELECT
statement returns a list that is grouped by one or more columns, usually in order to apply some sort of aggregate function to certain columns.
Examples:
Returns a list of Department IDs along with the sum of their sales for the date of January 1, 2000.
SELECT DeptID, SUM(SaleAmount) FROM Sales WHERE SaleDate = '01-Jan-2000' GROUP BY DeptID
Common grouping (aggregation) functions include:
- Count - Quantity of matching records (per group)
- Sum - Summation of given value (per group)
- Min - Minimum of given value (per group)
- Max - Maximum of given value (per group)
- Avg - Average of given value (per group)