Jump to content

Group by (SQL)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Itrummer (talk | contribs) at 19:19, 9 December 2020 (refined explanation of grouping semantics, considering requested clarifications; more details on the context in which grouping is applicable). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A GROUP BY statement 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.[1][2]

The result of a query using a GROUP BY statement 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."SQL Grouping and Aggregation". {{cite web}}: Missing or empty |url= (help); Text "http://www.databaselecture.com/sql.html" ignored (help)</ref>

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

Returns the data of the example pivot table which answers the question "How many Units did we sell in each Region for every Ship Date?":

   SELECT Region, Ship_Date, SUM(Units) AS Sum_of_Units
     FROM FlatData
 GROUP BY Region, Ship_Date


Common grouping (aggregation) functions include:

  • Count(expression) - Quantity of matching records (per group)
  • Sum(expression) - Summation of given value (per group)
  • Min(expression) - Minimum of given value (per group)
  • Max(expression) - Maximum of given value (per group)
  • Avg(expression) - Average of given value (per group)

References

  1. ^ "SQL GROUP BY Statement". www.w3schools.com. Retrieved 2020-09-18.
  2. ^ shkale-msft. "GROUP BY (Transact-SQL) - SQL Server". docs.microsoft.com. Retrieved 2020-09-18.

See also