SQL also supports the use of aggregate expressions (or functions) that allow you to summarize information about a group of rows of data.

SELECT AGG_FUNC(column_or_expression) AS aggregate_description, …
FROM mytable
WHERE constraint_expression;

Without a specified grouping, each aggregate function is going to run on the whole set of result rows and return a single value. And like normal expressions, giving your aggregate functions an alias ensures that the results will be easier to read and process.

FunctionDescription
COUNT(*)COUNT(column)A common function used to counts the number of rows in the group if no column name is specified. Otherwise, count the number of rows in the group with non-NULL values in the specified column.
MIN(column)Finds the smallest numerical value in the specified column for all rows in the group.
MAX(column)Finds the largest numerical value in the specified column for all rows in the group.
AVG(column)Finds the average numerical value in the specified column for all rows in the group.
SUM(column)Finds the sum of all numerical values in the specified column for the rows in the group.

Grouped aggregate functions

In addition to aggregating across all the rows, you can instead apply the aggregate functions to individual groups of data within that group (ie. box office sales for Comedies vs Action movies).
This would then create as many results as there are unique groups defined as by the GROUP BY clause.

Select query with aggregate functions over groups

SELECT AGG_FUNC(column_or_expression) AS aggregate_description, … FROM mytable WHERE constraint_expression GROUP BY column;

The GROUP BY clause works by grouping rows that have the same value in the column specified.

aggregates function with group by clause

Our queries are getting fairly complex, but we have nearly introduced all the important parts of a SELECT query. One thing that you might have noticed is that if the GROUP BY clause is executed after the WHERE clause (which filters the rows which are to be grouped), then how exactly do we filter the grouped rows?

Luckily, SQL allows us to do this by adding an additional HAVING clause which is used specifically with the GROUP BY clause to allow us to filter grouped rows from the result set.

syntax

SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias,
FROM mytable
WHERE condition
GROUP BY column
HAVING group_condition;

Leave a Reply

Your email address will not be published. Required fields are marked *