Group by (SQL)
A GROUP BY clause in SQL specifies that a SQL SELECT statement partitions result rows into groups, based on their values in one or several columns. Typically, grouping is used to apply some sort of aggregate function for each group. The result of a query using a GROUP BY clause contains one row for each group. This implies constraints on the columns that can appear in the associated SELECT clause. As a general rule, the SELECT clause may only contain columns with a unique value per group. This includes columns that appear in the GROUP BY clause as well as aggregates resulting in one value per group.
A GROUP BY clause in SQL specifies that a SQL SELECT statement partitions result rows into groups, based on their values in one or several columns. Typically, grouping is used to apply some sort of aggregate function for each group. The result of a query using a GROUP BY clause contains one row for each group. This implies constraints on the columns that can appear in the associated SELECT clause. As a general rule, the SELECT clause may only contain columns with a unique value per group. This includes columns that appear in the GROUP BY clause as well as aggregates resulting in one value per group. SELECT DeptID, SUM(SaleAmount) FROM Sales WHERE SaleDate = '01-Jan-2000' GROUP BY DeptID In the following example one can ask "How many units were sold in each region for every ship date?": The following code returns the data of the above pivot table which answers the question "How many units were sold in each region for every ship date?": SELECT Region, Ship_Date, SUM(Units) AS Sum_of_Units FROM FlatData GROUP BY Region, Ship_Date ==See also== Aggregate function ==References== ==External links== SQL Snippets: SQL Features Tutorials - Grouping Rows with GROUP BY
References
- SQL GROUP BY Statement · retrieved 2020-09-17 · rev 1323283523
Cite this reference
MLA "SQL GROUP BY Statement." 2020-09-17. https://www.w3schools.com/sql/sql_groupby.asp. Accessed 24 November 2026.
Chicago "SQL GROUP BY Statement." 2020-09-17. https://www.w3schools.com/sql/sql_groupby.asp.
APA encyclopegia.org. (2020). SQL GROUP BY Statement. Retrieved November 24, 2026, from https://www.w3schools.com/sql/sql_groupby.asp
- GROUP BY (Transact-SQL) - SQL Server · retrieved 2020-09-17 · rev 1323283523
Cite this reference
MLA "GROUP BY (Transact-SQL) - SQL Server." shkale-msft. 2020-09-17. https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql. Accessed 24 November 2026.
Chicago "GROUP BY (Transact-SQL) - SQL Server." shkale-msft. 2020-09-17. https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql.
APA shkale-msft. (2020). GROUP BY (Transact-SQL) - SQL Server. Retrieved November 24, 2026, from https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql
- SQL Grouping and Aggregation · retrieved 2020-12-08 · rev 1323283523
Cite this reference
MLA "SQL Grouping and Aggregation." 2020-12-08. http://www.databaselecture.com/sql.html. Accessed 24 November 2026.
Chicago "SQL Grouping and Aggregation." 2020-12-08. http://www.databaselecture.com/sql.html.
APA encyclopegia.org. (2020). SQL Grouping and Aggregation. Retrieved November 24, 2026, from http://www.databaselecture.com/sql.html
Cite this article
MLA
"Group by (SQL)." encyclopegia.org, 2025, https://encyclopegia.org/wiki/group-by-sql. Accessed 24 November 2026.
Chicago
"Group by (SQL)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/group-by-sql.
APA
encyclopegia.org. (2025). Group by (SQL). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/group-by-sql
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON