w3resource logo


postgresql avg functions

PostgreSQL AVG function

rating has average rating 5 out of 10. Total 2 users rated.

<<PreviousNext>>

Description

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

Syntax

AVG (* | [DISTINCT] ALL | column_name)

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

postgresql sample table employee example1

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 function example1

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 function as level name

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 function two columns

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 function with max,min,count and sum

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';
              

Output

postgresql avg function where clause



<<PreviousNext>>