×

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value.

Here, multiple rows of data are grouped and form a single data.

Types of Aggregate Operators

In SQL, there are five types of aggregate functions/operators to perform the operations on certain data.

  1. sum()
  2. avg()
  3. max()
  4. min()
  5. count()

In SQL, The aggregate functions ignore the NULL values when it performs the calculations on multiple rows, except for the count() operator.

In this concept, we can also use the aggregate operators with the group by ang having classes to combine the data of the select statement.

Sum() Operators

The sum() function gives the total sum of the selected columns of a table

This operator mainly works on the numeric columns only.

Syntax:

sum()

Or

sum([all | distincts]expression)

Example: Let’s take an employee table and perform the sum() operation on it

IDNAMEAGESALARY
11Jack3010000
12John3220000
13Jill3530000
14Rose3740000
15James4050000
13Jill3340000
16Alex3370000

Query: Select sum(salary) from employee;

Output:

SUM(SALARY)
260000

Query: Select sum(eid) from employee;

Output:

SUM(ID)
94

Query: Select sum(age) from employee;

Output:

SUM(AGE)
240

Using group by:

  • Select sum(salary), age from employee group by age;
SUM(SALARY)AGE
10000
20000
30000
40000
50000
110000
30
32
35
37
40
33
  • Select sum(salary), id from employee group by id;
SUM(SALARY)AGE
10000
70000
40000
20000
50000
70000
11
13
14
12
15
16
  • Select sum(salary), name from employee group by name;
SUM(SALARY)NAME
50000
20000
70000
40000
70000
10000
James
John
Jill
Rose
Alex
Jack

Using having clause:

  • Select sum(salary), idfrom employee1 group by id having sum(salary)>40000;
SUM(SALARY)ID
70000
50000
80000
13
15
16
  • Select sum(age) from employee group by id having sum(id)>12;
SUM(ID)NAME
15
26
14
16
James
jill
rose
Alex

Avg() Operators

The avg() function gives the total average of the selected columns of a table

This operator mainly works on the numeric columns, and it returns the average of all non-null values

Syntax:

avg()

Or

avg([all | distincts]expression)

Example: Let’s take an employee table and perform the avg() operation on it

IDNAMEAGESALARY
11Jack3010000
12John3220000
13Jill3530000
14Rose3740000
15James4050000
13Jill3340000
16Alex3370000

Query: Select avg(salary) from employee;

Output:

AVG(SALARY)
37142.8571

Query: Select avg(id) from employee;

Output:

AVG(ID)
13.4285714

Query: Select avg(age) from employee;

Output:

AVG(AGE)
34.2857143

Using group by:

  • Select avg(salary), age from employee group by age;
AVG(SALARY)AGE
10000
20000
30000
40000
50000
550000
30
32
35
37
40
33
  • Select avg(salary), id from employee group by id;
AVG(SALARY)AGE
10000
35000
40000
20000
50000
70000
11
13
14
12
15
16
  • Select avg(salary), name from employee group by name;
SUM(SALARY)NAME
50000
20000
35000
40000
70000
10000
James
John
Jill
Rose
Alex
Jack

Using having clause:

  • Select avg(salary), id from employee1 group by id  having avg(salary)>40000;
AVG(SALARY)ID
50000
70000
15
16
  • Select avg(id),name from employee group by name having avg(id)>12;
AVG(ID)NAME
15
13
14
16
James
Jill
Rose
Alex

Max() Operators

The max() function gives the highest or maximum value of the selected columns of a table, and it is a set of all non-null values.

Syntax:

max()

Or

max([all | distincts]expression)

Example:

Let’s take an employee table and perform the max() operation on it

IDNAMEAGESALARY
11Jack3010000
12John3220000
13Jill3530000
14Rose3740000
15James4050000
13Jill3340000
16Alex3370000

Query: Select max(salary) from employee;

Output:

MAX(SALARY)
70000

Query: Select  max(id) from employee;

Output:

MAX(ID)
16

Query: Select max(age) from employee;

Output:

MAX(AGE)
40

Using group by:

  • Select max(salary), age from employee group by age;
MAX(SALARY)AGE
10000
20000
30000
40000
50000
70000
30
32
35
37
40
33
  • Select max(salary), id from employee group by id;
MAX(SALARY)AGE
10000
40000
40000
20000
50000
70000
11
13
14
12
15
16
  • Select max(salary), name from employee group by name;
MAX(SALARY)NAME
50000
20000
40000
40000
70000
10000
James
John
Jill
Rose
Alex
Jack

Using having clause:

  • select max(salary), id from employee1 group by id having max(salary)>40000;
MAX(SALARY)ID
50000
70000
15
16
  • Select max(id), name from employee group by name having max(id)>12;
AVG(ID)NAME
15
13
14
16
James
Jill
Rose
Alex

Min() Operators

The min() function gives the lowest or minimum value of the selected columns of a table, and it is a set of all non-null values.

Syntax:

min()

Or

max([all | distincts]expression)

Example:

Let’s take an employee table and perform the min() operation on it

IDNAMEAGESALARY
11Jack3010000
12John3220000
13Jill3530000
14Rose3740000
15James4050000
13Jill3340000
16Alex3370000

Query: Select max(salary) from employee;

Output:

MIN(SALARY)
10000

Query: Select max(id) from employee;

Output:

MIN(ID)
11

Query: Select max(age) from employee;

Output:

MIN(AGE)
30

Using group by:

  • Select min(salary), age from employee group by age;
MAX(SALARY)AGE
10000
20000
30000
40000
50000
70000
30
32
35
37
40
33
  • Select min(salary), id from employee group by id;
MAX(SALARY)AGE
10000
40000
40000
20000
50000
70000
11
13
14
12
15
16
  • Select min(salary), name from employee group by name;
MIN(SALARY)NAME
50000
20000
30000
40000
70000
10000
James
John
Jill
Rose
Alex
Jack

Using having clause:

  • select min(salary), id from employee1 group by id  having min(salary)>40000;
MIN(SALARY)ID
50000
70000
15
16
  • Select max(id), name from employee group by name having min(id)>12;
AVG(ID)NAME
15
13
14
16
James
Jill
Rose
Alex

Count() Operators

The count() function gives the number of rows in the table. It is a set  of all non-null values

Syntax:

count()

Or

count([all |distincts]expression)

Example:

Let’s take an employee table and perform the count() operation on it

IDNAMEAGESALARY
11Jack3010000
12John3220000
13Jill3530000
14Rose3740000
15James4050000
13Jill3340000
16Alex3370000

Query: Select count(salary) from employee;

Output:

COUNT(SALARY)
7

Query: Select count(id) from employee;

Output:

COUNT(ID)
7

Query: Select count(age) from employee;

Output:

COUNT(AGE)
7

Using group by:

  • Select max(salary), age from employee group by age;
MAX(SALARY)AGE
1
1
1
1
1
2
30
32
35
37
40
33
  • Select max(salary), id from employee group by id;
MAX(SALARY)AGE
1
1
1
1
1
1
11
13
14
12
15
16
  • Select max(salary), name from employee group by name;
MAX(SALARY)NAME
1
1
1
1
1
1
James
John
Jill
Rose
Alex
Jack

Using having clause:

  1. select count(salary), id from employee1 group by id having count(salary)>40000;
    no rows selected
  2. Select max(id), name from employee group by name having max(id)>12;
    no rows selected

Related Topics

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 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.

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...

4 minutes read.

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.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

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.

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.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 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 Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.