# PostgreSQL AVG function

## Description

The AVG function determines the average of all selected values of a column.

## Syntax

## Parameters

Name |
Description |
---|---|

column_name | Name of the column |

* | The asterisk(*) indicates all the rows. |

DISTINCT | This clause is optional. It indicates uniqueness. |

ALL | This clause is optional. It is default clause. |

## PostgreSQL AVG function example

## The sample table

If we want to find the average salary from all employees in *employee* table, the following SQL can be used.

## SQL

SELECT AVG(salary) FROM employee;

## Output

## PostgreSQL AVG as a level

If we want to get the average salary for all employees and show the result against 'Average Salary' head in *employee* table, the following SQL can be used.

## SQL

SELECT AVG(salary) "Average Salary" FROM employee;

OR

SELECT AVG(DISTINCT salary) "Average Salary" FROM employee;

OR

SELECT AVG(ALL salary) "Average Salary" FROM employee;

## Output

## PostgreSQL AVG two columns

If we want to get the average salary and deduction from *employee* table, the following SQL can be used.

## SQL

SELECT AVG(salary) "Average Salary" , AVG(deduction) "Average Deduction" FROM employee;

## Output

## PostgreSQL AVG with MAX, MIN, COUNT and SUM

If we want to get the average salary, maximum salary, minimum salary, total employee and total salary from *employee* table, the following SQL can be used.

## SQL

SELECT AVG(salary) "Average Salary" , MAX(salary) "Maximum Salary", MIN(salary) "Minimum Salary", COUNT(*) "Total Employee", SUM(salary) "Total Salary" FROM employee;

## Output

## PostgreSQL AVG WHERE clause

If we want to get the average salary and deduction from *employee* table who belongs to the designation 'SALESMAN', the following SQL can be used.

## SQL

SELECT AVG(salary) "Average Salary" , AVG(deduction) "Average Deduction" FROM employee WHERE designame='SALESMAN';