Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

Aggregate Functions in Relational Database






Aggregate Function or Aggregation Function in Relational Database is a function which returns a single Summary Value from the values of multiple rows grouped together.


Some of the mostly used Aggregate Functions are discussed below

1. COUNT

Basic Syntax:
Here is the Syntax to count all the rows in a Table;

Select COUNT(column name) from Table;

Example: 
The following query counts all the rows from tbl_employee whose name column is not empty.

Select COUNT(name) from tbl_employee;

Basic Syntax with condition:
Here is the Syntax to Count total rows in specific condition. WHERE keyword is used to add the condition in query.

Select COUNT(column name) from Table WHERE <condition>;

Example with Condition: 
The query below counts the total number of employees whose address is 'Kathmandu'.

Select COUNT(id) from Table WHERE address='Kathmandu';


2. MAX

MAX aggregate function is used to find the Greatest or Highest numerical value in a column.
(It doesn't work on string value)

Basic Syntax:
SELECT MAX(column) FROM TABLE

Example: 
Following query will find the Maximum Salary from 'tbl_employee' Table.
SELECT MAX(salary) FROM tbl_employee

3. MIN

MIN aggregate function is used to find the Lowest or Smallest numerical value in a column.
(It doesn't work on string value)

Basic Syntax:
SELECT MIN(column) FROM TABLE

Example: 
Following query will find the Minimum Salary from 'tbl_employee' Table.

SELECT MIN(salary) FROM tbl_employee



4. SUM

SUM aggregate function is used to find the Total Sum of a column.
(It doesn't work on string value)

Basic Syntax:
SELECT SUM(column) FROM TABLE;

Example: 
Following query will find the Total Salary of All Employeese.

SELECT SUM(salary) FROM tbl_employee;

5. AVG

AVG aggregate function is used to find the Average or Mean value of a column.
(It doesn't work on string value)

Basic Syntax:
SELECT AVG(column) FROM TABLE;

Example: 
Following Query will find the Average age of the Employees.

SELECT AVG(age) FROM tbl_employee;



Do you want to learn Database?

Here's a "Database for Beginners" FREE Course for you.

Get Access Now



Comments