PostgreSQL - COUNT(), SUM() & AVG() Functions
The PostgreSQL COUNT() function returns the count of an expression.
The PostgreSQL SUM() function returns the summed value of an expression.
The PostgreSQL AVG() function returns the average value of an expression.
PostgreSQL COUNT() Function
The PostgreSQL COUNT() function returns the count of an expression.
Syntax
The syntax for using COUNT() function in PostgreSQL is given below:
SELECT COUNT(column_name) FROM table_name WHERE condition(s);
The PostgreSQL GROUP BY clause is used to arrange result table into identical groups when one or more columns are used. Please note that it is must to include those column names in a GROUP BY clause which are not encapsulated within the COUNT() function. See the syntax below:
SELECT column1, column2, ... COUNT(column_name) FROM table_name WHERE condition(s) GROUP BY column1, column2, ...;
Example - With Single Column
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
To get the count of employees whose age is greater than 25, the following query can be used:
SELECT COUNT(Name) AS EmployeeGT25 FROM Employee WHERE Age > 25;
This will produce the result as shown below:
EmployeeGT25 |
---|
4 |
Example - Using DISTINCT
The DISTICT clause can be used with COUNT() function. For example - To get the count of distinct (unique) age of the employees, the following code can be used:
SELECT COUNT(DISTINCT Age) AS DistinctAge FROM Employee;
This will produce the result as shown below:
DistinctAge |
---|
5 |
Example - Using GROUP BY
To get the count of employees group by their age, the following query can be used:
SELECT Age, COUNT(Age) AS NumberOfEmployee FROM Employee GROUP BY Age;
This result of the above code will be:
Age | NumberOfEmployee |
---|---|
24 | 1 |
25 | 1 |
27 | 1 |
28 | 2 |
30 | 1 |
PostgreSQL SUM() Function
The PostgreSQL SUM() function returns the summed value of an expression.
Syntax
The syntax for using SUM() function in PostgreSQL is given below:
SELECT SUM(column_name) FROM table_name WHERE condition(s);
The PostgreSQL GROUP BY clause is used to arrange result table into identical groups when one or more columns are used. Please note that it is must to include those column names in a GROUP BY clause which are not encapsulated within the SUM() function. See the syntax below:
SELECT column1, column2, ... SUM(column_name) FROM table_name WHERE condition(s) GROUP BY column1, column2, ...;
Example - With Single Column
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
To get the combined total salary of all employees whose age is greater than 25, the following query can be used:
SELECT SUM(Salary) AS TotalSalary FROM Employee WHERE Age > 25;
This will produce the result as shown below:
TotalSalary |
---|
11700 |
Example - Using DISTINCT
The DISTICT clause can be used with SUM() function. For example - To get the combined total salary of distinct (unique) salaries, the following code can be used:
SELECT SUM(DISTINCT Salary) AS TotalUniqueSalary FROM Employee;
This will produce the result as shown below:
TotalUniqueSalary |
---|
11650 |
Example - Using Formula
The expression contained within the SUM() function does not need to be a single field. A formula can also be used with this function. For example - Consider a bonus (15% of Salary) is given to each employee, and to calculate the total bonus amount, the following query can be used:
SELECT SUM(Salary * 0.15) AS TotalBonusAmount FROM Employee;
This will produce the result as shown below:
TotalBonusAmount |
---|
2617.5 |
Example - Using GROUP BY
To get the combined total salary of employees group by their age, the following query can be used:
SELECT Age, SUM(Salary) AS TotalSalaryByAge FROM Employee GROUP BY Age;
This result of the above code will be:
Age | TotalSalaryByAge |
---|---|
24 | 2750 |
25 | 3000 |
27 | 2800 |
28 | 5800 |
30 | 3100 |
PostgreSQL AVG() Function
The PostgreSQL AVG() function returns the average value of an expression.
Syntax
The syntax for using AVG() function in PostgreSQL is given below:
SELECT AVG(column_name) FROM table_name WHERE condition(s);
The PostgreSQL GROUP BY clause is used to arrange result table into identical groups when one or more columns are used. Please note that it is must to include those column names in a GROUP BY clause which are not encapsulated within the AVG() function. See the syntax below:
SELECT column1, column2, ... AVG(column_name) FROM table_name WHERE condition(s) GROUP BY column1, column2, ...;
Example - With Single Column
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
To get the average salary of employee with age greater than 25, the following query can be used:
SELECT AVG(Salary) AS AvgSalary FROM Employee WHERE Age > 25;
This will produce the result as shown below:
AvgSalary |
---|
2925.0 |
Example - Using DISTINCT
The DISTICT clause can be used with AVG() function. For example - To get the average value of distinct (unique) salaries, the following code can be used:
SELECT AVG(DISTINCT Salary) AS AvgUniqueSalary FROM Employee;
This will produce the result as shown below:
AvgUniqueSalary |
---|
2912.5 |
Example - Using Formula
The expression contained within the AVG() function does not need to be a single field. A formula can also be used with this function. For example - Consider a bonus (15% of Salary) is given to each employee, and to calculate the average of bonus amount, the following query can be used:
SELECT AVG(Salary * 0.15) AS AvgBonusAmount FROM Employee;
This will produce the result as shown below:
AvgBonusAmount |
---|
436.25 |
Example - Using GROUP BY
To get the average salary of employees group by their age, the following query can be used:
SELECT Age, AVG(Salary) AS AvgSalaryByAge FROM Employee GROUP BY Age;
This result of the above code will be:
Age | AvgSalaryByAge |
---|---|
24 | 2750.0 |
25 | 3000.0 |
27 | 2800.0 |
28 | 2900.0 |
30 | 3100.0 |