The SQL GROUP BY is a clause enables SQL aggregate functions for a grouping of information. For example, it could support subtotaling by region number in a sales table.
GROUP BY supports dividing data into sets so that aggregate functions like SUM, AVG and COUNT can be performed.
The SQL GROUP BY clause is used whenever aggregate functions by group are required. This is an aid to understanding and analyzing information.
SQL GROUP BY is used as follows. It must follow the FROM and WHERE clauses. The columns in a SELECT clause must be either group by columns or aggregate function columns.
SQL GROUP BY Syntax
SQL GROUP BY Example
The following example produces a count of branches by region_nbr from the branch table.
Here are the contents of the table:
Table: BRANCH
branch_nbr | branch_name | region_nbr | employee_count |
108 | New York | 100 | 10 |
110 | Boston | 100 | 6 |
212 | Chicago | 200 | 5 |
404 | San Diego | 400 | 6 |
415 | San Jose | 400 | 3 |
This SQL Statement with GROUP BY is executed:
Here is the result.
region_nbr | count(*) |
100 | 2 |
200 | 1 |
400 | 2 |
Advertisements:
Infogoal.com is organized to help you gain mastery.
Examples may be simplified to facilitate learning.
Content is reviewed for errors but is not warranted to be 100% correct.
In order to use this site, you must read and agree to the
terms of use, privacy policy and cookie policy.
Copyright 2006-2020 by Infogoal, LLC. All Rights Reserved.