×

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples.

SQL Aggregate Function is a function used to perform calculation operations on one or more than one row of the single column of the table to return single data or multiple data in the output result sets.

The SQL Aggregate Function is used with the GROUP BY clause and the HAVING clause to group the SELECT query result.

The function which falls under the SQL aggregate functions is as follows:

1 Min() Function.

2 Max() Function.

3 Avg() Function.

4 Count() Function.

5 Sum() Function.

The NULL value is ignored by all the above functions like Min(), Max(), Sum(), and Avg() except Count() function.

The above Function ignored duplicate values if the distinct keyword is used in the Function. Else, duplicate records can't be ignored.

Now, let's learn about the SQL aggregate function concept with the help of syntax and examples.

Consider the already existing table name Diploma_Student:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakashi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845

1 Count() Functions: The Count() Function displays the total number of data or rows from the table. The Count() function works with integer data and string data.

For example, Suppose I want to count how many students are present in the classroom whose first name start with the letter 'P'? I will search in the attendance sheet student's name starts with the letter 'P' and write it somewhere on the paper.

The syntax of the Count() Function is as follows:

SELECT Column_Name1, Column_Name2, Count(Column_Name) FROM Table_Name;

The above count function displays the total number of records of the specific columns, including null values.

Count(*) function returns all the table rows, including duplicates and NULL data.

Example 1: Execute a query to count the number of students who scored above 80 percent in the Sixth Sem.    

SELECT Count(Sixth_sem) AS 'Total Number of Students' FROM Diploma_Student WHERE Sixth_sem > 80;

In the above Count() query example, we return the total number of the students who scored above 80 percent in the sixth Sem.

The output of the above query is as follows:

Total Number of Students
8
SQL Aggregate Functions

Eight students scored greater than 80% in the sixth Sem.

Example 2: Execute a query to display the total number of the student whose names start with the letter 'A'

SELECT Count(Student_Name) AS 'Total Number' FROM Diploma_Student WHERE Student_Name LIKE 'A%';

In the above Count() query example, we return the total number of students whose names start with 'A'.

The output of the above query is as follows:

Total Number
5
SQL Aggregate Functions

There are five students whose name starts with the letter 'A'.

2 Sum() Functions: The Sum() Function is used to calculate the column's values.

The syntax of the Sum() Function is as follows:

SELECT Column_Name1, Column_Name2, Sum(Column_Name) FROM Table_Name;

Example 1: Execute a query to calculate the first Sem and the Sixth Sem sum.

SELECT SUM(First_Sem) AS 'Sum of First Sem ', SUM(Sixth_Sem) AS 'Sum of Sixth Sem ' FROM Diploma_Student;

In the Sum() function query example, we display the sum of the first sem column and the sum of the sixth sem column.

The output of the above query is as follows:

Sum of First SemSum of Sixth Sem
12421244
SQL Aggregate Functions

Example 2: Execute a query to calculate the sum of all the columns from First Sem to Sixth Sem where student id is 202115.

SELECT SUM(First_Sem + Second_Sem + Third_Sem + Fourth_Sem + Fifth_Sem + Sixth_Sem)  AS 'Total Without Average' FROM Diploma_Student WHERE Student_Id = 202115;

In the above Sum() function query example, we display the sum of all the columns mentioned in the query where the student Id is 201115.

The output of the above query is as follows:

Total Without Average
509
SQL Aggregate Functions

3 Avg() Functions: The Avg() function is used to return the average value of the selected columns.

The syntax of the Avg() Function is as follows:

SELECT Column_Name1, Column_Name2, Avg(Column_Name) FROM Table_Name;

Example: Execute a query to return the average of the second Sem column.

SELECT AVG(Second_Sem)  AS ' Average of Second Sem' FROM Diploma_Student;

In the above Avg() function query example, we return the avg of the second Sem column.

The output of the above query is as follows:

Average of Second Sem
82.1333

4 Min() Function: The Min() Function is used to return the smallest value of the column.

The syntax of the Min() Function is as follows:

SELECT Column_Name1, Column_Name2, Min(Column_Name) FROM Table_Name;

Example: Execute a query to find the smallest value of the Third Sem.

SELECT MIN(Third_Sem)  AS 'Minimum Value' FROM Diploma_Student;

In the above Min() function query example, we return the smallest value of the third sem column.

The output of the above query is as follows:

Minimum Value
71
SQL Aggregate Functions

5 Max() Function: The Max() Function is used to return the largest value of the column.

The syntax of the Max() Function is as follows:

SELECT Column_Name1, Column_Name2, Max(Column_Name) FROM Table_Name;

Example: Execute a query to find the smallest value of the Third Sem.

SELECT MAX(Fourth_Sem)  AS 'Maximum Value' FROM Diploma_Student;

In the above max() function query example, we return the largest value of the fourth sem column.

The output of the above query is as follows:

Maximum Value
90
SQL Aggregate Functions

Related Topics

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.