## Introduction to Oracle Aggregate Functions

Oracle Aggregate function is a type of function which operates on specified column and returns a single row result. These functions are mostly used in a query which contains GROUP BY clause in a SELECT statement where GROUP BY clause groups the rows as per specified condition and AGGREGATE function performs aggregation on the grouped data.

- It returns a single row result based on groups of rows conditions.
- Aggregate functions can be used in SELECT statement.
- Aggregate functions can be used in HAVING clause as a condition.
- It returns one result per group as an output.
- The group of rows can be the whole table or the rows split into many groups.

**Important Aggregate Functions:**

Functions |
Description |

AVG ( ) | It returns an average value of given expression. |

SUM ( ) | It returns the sum value of given expression. |

MAX ( ) | It returns maximum value of given expression. |

MIN ( ) | It returns minimum value of given expression. |

COUNT ( ) | It returns total no of rows for the given expression. |

STDDEV ( ) | It returns the standard deviation of the column. |

VARIANCE ( ) | It returns the variance. |

### Syntax of Oracle Aggregate Functions

Given below is the syntax :

`GroupFunctionName (DISTINCT / ALL ColumnName)`

**Guidelines to Use:**

- DISTINCT keyword is used to ignore duplicate values and function consider only non-duplicate values.
- ALL keyword is used to consider all values for the aggregate operation including duplicates.
- CHAR, VARCHAR2, NUMBER or DATE data types can be used for argument.
- All group function except COUNT (*) function ignore NULLs. The NVL function can be a better option to substitute a value for NULLs.
- No single row column(s) should be used with group function in SELECT statement without GROUP BY clause.

### Examples of Oracle Aggregate Functions

Given below are the examples mentioned:

We will use the below sample table (Employee) with 14 records to see the Oracle Aggregate function behavior.

**Code:**

`SELECT * FROM Employee;`

**Output:**

#### Example #1

AVG (DISTINCT / ALL ColumnName) Function.

- Output will be the Average value of column.
- It doesn’t consider NULL values.

**Code:**

`SELECT AVG(Salary), AVG(DISTINCT Salary) FROM Employee;`

**Output:**

In the above example, there are two average values of the salary column and both are different because first average function operates on all values of the column but the second average function operates on unique values because of DISTINCT keyword. That’s why both results are different.

**Code:**

`SELECT AVG(Bonus) FROM Employee;`

**Output:**

In the above example, there is an average value of the Bonus column and bonus column contains NULL values. The AVG function ignores NULL values and calculate the average of the not null values.

#### Example #2

SUM (DISTINCT / ALL ColumnName) Function.

- Output will be the SUM value of column.
- It doesn’t consider NULL values.

**Code:**

`SELECT SUM (Salary), SUM (DISTINCT Salary) FROM Employee;`

**Output:**

In the above example, there are two sum values of the salary column and both are different because first sum function operates on all values of the column but the second sum function operates on unique values because of DISTINCT keyword. That’s why both results are different.

**Code:**

`SELECT SUM (Bonus) FROM Employee;`

**Output:**

In the above example, there is one sum value of the Bonus column and bonus column contains NULL values. The SUM function ignores NULL values and calculate the sum of not null values.

#### Example #3

MAX (DISTINCT / ALL ColumnName) Function.

- Output will be the maximum value of column.
- It doesn’t consider NULL values.

**Code:**

`SELECT MAX(Salary), MAX(DISTINCT Salary) FROM Employee;`

**Output:**

In the above example, there are two maximum values of the salary column and both are same but first maximum function operates on all values of the column but the second maximum function operates on unique values because of DISTINCT keyword and returns an appropriate output.

4.5 (7,682 ratings)

View Course

**Code:**

`SELECT MAX(Bonus) FROM Employee;`

**Output:**

In the above example, there is a maximum value of the Bonus column and bonus column contains NULL values. The MAX function ignores NULL values and returns the maximum value of that column.

#### Example #4

MIN (DISTINCT / ALL ColumnName) Function.

- Output will be the minimum value of column.
- It doesn’t consider NULL values.

**Code:**

`SELECT MIN (Salary), MIN (DISTINCT Salary) FROM Employee;`

**Output:**

In the above example, there are two minimum values of the salary column and both are same but first minimum function operates on all values of the column but the second minimum function operates on unique values because of DISTINCT keyword and returns an appropriate output.

**Code:**

`SELECT MIN (Bonus) FROM Employee;`

**Output:**

In the above example, there is a minimum value of the Bonus column and bonus column contains NULL values. The MIN function ignores NULL values and returns the minimum value of that column.

#### Example #5

STDDEV (DISTINCT / ALL ColumnName) Function.

- Output will be the Standard Deviation of column.
- It doesn’t consider NULL values.

**Code:**

`SELECT STDDEV(Salary), STDDEV(DISTINCT Salary) FROM Employee;`

**Output:**

In the above example, there are two Standard Deviation values of the salary column and both are different because first Standard Deviation function operates on all values of the column but the second Standard Deviation function operates on unique values because of DISTINCT keyword. That’s why both results are different.

**Code:**

`SELECT STDDEV(Bonus) FROM Employee;`

**Output:**

In the above example, there is a Standard Deviation value of the Bonus column and bonus column contains NULL values. The STDDEV function ignores NULL values and calculate the Standard Deviation of the not null values.

#### Example #6

VARIANCE (DISTINCT / ALL ColumnName) Function.

- Output will be the Variance of N.
- It doesn’t consider NULL values.

**Code:**

`SELECT VARIANCE(Salary), VARIANCE(DISTINCT Salary) FROM Employee;`

**Output:**

In the above example, there are two Variance values of the salary column and both are different because first variance function operates on all values of the column but the second variance function operates on unique values because of DISTINCT keyword. That’s why both results are different.

**Code:**

`SELECT VARIANCE (Bonus) FROM Employee;`

**Output:**

In the above example, there is a variance value of the Bonus column and bonus column contains NULL values. The VARIANCE function ignores NULL values and calculate the variance of the not null values.

#### Example #7

COUNT (*/ DISTINCT / ALL ColumnName) Function.

- Output will be the number of rows.
- With COUNT function, * is used to return all rows including duplicates and NULLs.
- It is used to get the count of all rows or distinct values of column.

**Code:**

`SELECT COUNT(*) FROM Employee;`

**Output:**

In the above example, COUNT function returns 14 because * considers all rows including duplicates and NULLs.

**Code:**

`SELECT COUNT(Designation), COUNT(DISTINCT Designation) FROM Employee;`

**Output:**

In the above example, returns two count values of the Designation column and both are different because first count function operates on all values of the column but the second count function operates on unique values because of DISTINCT keyword and returns an appropriate output.

**Code:**

`SELECT COUNT(Bonus) FROM Employee;`

**Output:**

In the above example, there is a count value of the Bonus column and bonus column contains NULL values. The COUNT function ignores NULL values and count only not null values.

#### Example #8

GROUP BY Clause with Aggregate Function.

- GROUP BY clause is used for grouping the column.
- If any aggregate function is included in a SELECT statement with non-group functional columns then it is used for grouping the non-group column(s).

**Code:**

`SELECT Deptnumber, MAX (Salary) FROM Employee;`

**Output:**

The above example, returns error because there is a non-group functional column is used with an aggregate function MAX. So the SELECT statement returns all rows for Deptnumber column but aggregate function returns only one row. To avoid the error GROUP BY clause can be used.

**Code:**

`SELECT Deptnumber, MAX (Salary) FROM Employee GROUP BY Deptnumber;`

**Output:**

**Tip: **These functions can be used on any data type.

### Conclusion

Oracle Aggregate Functions are very useful to calculate on a group of rows and return a single value for each group. It allows us to summarize data from multiple rows of a table or view.

### Recommended Articles

This is a guide to Oracle Aggregate Functions. Here we discuss the introduction to Oracle Aggregate Functions along with examples for better understanding. You may also have a look at the following articles to learn more –