logo

Aggregate Functions

Today, we will learn commonly used aggregate functions which are very handy for aggregation and statistical analysis.

COUNT()

The function returns the number of rows that matches a specific condition of a query.

There are different ways to use the function.

COUNT(*)

function returns the number of rows in the table, including NULL and duplicate rows.

Bear in mind that using the function on a large data set slows down the query because SQL must scan all the rows for all columns.

Example


COUNT(column)

The function returns the number of rows in the specific column. However, it does not consider NULL values in the column.

Example


COUNT(DISTINCT column)

The function returns the number of unique non-null values in the column.

Example


COUNT() with multiple columns

Example


COUNT() with condition

Example



SUM()

returns the sum of values or distinct values. However, It ignores NULL values.

Example



AVG()

function calculate the average value of a column.

Example



MIN(), MAX()

The function returns the minimum value of a column and the function returns the maximum value of a column.

Example



Next Lesson

Filtering with LIKE and Pattern Matching