bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL GROUP BY Statement

Concept visual

SQL GROUP BY Statement

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

The SQL GROUP BY Statement

The GROUP BY

statement is used to group rows that have the same values into summary rows, like "Find the number of customers in each country".

The GROUP BY

statement is almost always used in conjunction with aggregate functions, like COUNT(), MAX(), MIN(), SUM(), AVG(), to perform calculations on each group.

GROUP BY Syntax

Select

column1, aggregate_function(column2), column3, ...

From

table_name

Where

condition

Group By

column1, column3

Order By

column_name

;

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

CustomerID

CustomerName

ContactName

Address

City

PostalCode

Country

Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin

12209

Germany

Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222 México D.F. 05021

Mexico

Antonio Moreno Taquería

Previous

SQL UNION ALL Operator

Next

SQL HAVING Clause