The GROUP BY clause groups records into summary row(s). GROUP BY returns one records for each group.
The GROUP BY statement is often used to return a result-set with aggregate functions (COUNT, MAX, MIN, SUM, AVG) for one or more columns.
SQL GROUP BY Syntax:
SELECT Column1, Column2, Column3
FROM Table_Name
GROUP BY Column1
Example:
Lets use the “Products” table to count the number of products for each supplier.
| ProductID | ProductName | Description | Price | SupplierName | SupplierID |
|---|---|---|---|---|---|
| 1 | Super Printer | 9000 series printer | 300 | TechOne | 1 |
| 2 | Laptop 5 | 4000 Laptop 5th generation | 700 | TechTwo | 2 |
| 3 | 3D Printer | 3D Print Pro | 900 | TechOne | 1 |
| 4 | Labtop 7 | 100 Labtop 7 processor | 1200 | TechTwo | 2 |
| 5 | Camera 400 | Camera Ultra 400 | 400 | TechThree | 3 |
SELECT
Count (SupplierName) as Count,
SupplierName
FROM Products
GROUP BY SupplierName
Results:
| Count | SupplierName |
|---|---|
| 2 | TechOne |
| 2 | TechTwo |
| 1 | TechThree |
Get a free SQL Code Reference Here
SQL Tutorial
Featured
Select Statement