Home SQL learning 3 - Aggregation
Post
Cancel

SQL learning 3 - Aggregation

Update Date: 2024-03-06

Aggregation Statement

COUNT

1
select COUNT(column1) from tableName

COUNT returns the number of valid rows. (not null) Also, we can use 1 or * to represent all columns to return number of columns:

1
select COUNT(1) from tableName

SUM

1
select SUM(column1) from tableName

SUM return sum of all values in column1.

MAX / MIN

1
2
select MAX(column1) from tableName
select MIN(column1) from tableName

MAX/MIN return max/min of all values in column1.

AVG

1
select AVG(column1) from tableName

AVG return average of all values in column1. If where is added at the end, the AVG returns conditional mean.

ROUND

1
select ROUND(column1, decimalPlaces) from tableName

This returns rounded values. If decimalPlaces are omitted, it returns rounded with zero decimals.

HAVING

1
2
3
select AVG(column1) from tableName
group by column2
having condition

A conditional statement similar to WHERE but used with aggregate functions (COUNT(), MIN(), MAX(), SUM(), AVG()). The query above means that it returns averaged of column1 given different values of column2 that under condition.

This post is licensed under CC BY 4.0 by the author.