group by and having Clauses

Used in select statements to divide a table into groups and to return only groups that match conditions in the having clause. group by is typically used with aggregates to specify how to group the unaggregated columns of a select query. having clauses are applied to these groups.

Syntax

Start of select statement
[group by [all] aggregate_free_expression
	[, aggregate_free_expression]...]
[having search_conditions]
End of select statement

Parameters

Examples

Usage

See also Transact-SQL Functions in Reference Manual: Building Blocks.

Standards

ANSI SQL – Compliance level: Entry-level compliant.

The use of columns within the select list that are not in the group by list and have no aggregate functions is a Transact-SQL extension.

The use of the all keyword is a Transact-SQL extension.

Related reference
Transact-SQL Extensions to group by and having
compute Clause
declare
select
where clause