Skip to main content

How to use Oracle Aggregate Functions?

Aggregate functions are multi-row oracle functions, that operate on a set of rows and return one result or one result per group.

Aggregate functions can help you to answer some basic and complex questions.

Examine the below table named EMPLOYEES.



And below are some questions that could be answered by using appropriate Oracle aggregate functions using SQL query.

How many employees are working in the company?

What is the average salary for employees provided by the company?
What is the maximum or minimum salary provided by the company, and to whom?



Read more about



Types of Aggregate functions

  • COUNT 

  • AVG 

  • SUM 

  • MAX 

  • MIN 


Each function takes an argument and returns the single output for each row

SQL Count() aggregate function

There are two types of Oracle COUNT () aggregate function

  • count(c) :c is a column of which you want to count the record

  • count(*): It counts all the records of a table including the null values.


NOTE: All the group functions ignore null values except COUNT(*) aggregate function.

Examples

Consider table named employees for practicals



Now we will use the count aggregate function to count the total number of records in column salary of above table, let's see how many records it show

It is showing only 4 records because it ignores the null value

Using SQL COUNT(*) aggregate function

This function ignores the null values and counts the total number of records from a table.

Using SQL AVG() function

It finds the average of a given column's value, it works only with the numeric data.

Examples


Using SQL SUM() aggregate function

This function is used to find the total sum of the column values, It also works only with numeric data.

Example




NOTE: TOTAL_SALARY_PROVIDED is a column alias which we have provided for complex expression.

Using SQL MAX/MIN aggregate function

As the name suggests, these aggregate functions are used to retrieves the highest and lowest value from a specified column.




Example



Using GROUP BY clause

The Group by clause is used with the Oracle aggregate functions (that we have learned above) to group the result set by one or multiple columns

Syntax


SQL>SELECT col_1,col_2,col_3,GROUP_FUNCTION(col_4)
FROM table_name
WHERE condition
GROUP BY col_1,col_2,col_3;




Note: All the columns or expressions specified in the SELECT list must be placed in GROUP BY clause except grouped columns

GROUP BY clause can be used with all of the aggregate function

Examples




Consider table EMPLOYEES2 for practice






Using GROUP BY clause with MAX AGGREGATE function



Now to understand more clearly, just focus on above query and given description




As first the query selects the two columns named DEPT and HIGHEST SALARY (HIGHEST_SALARY is not the actual column but a column alias of specified expression i.e MAX(SALARY)) from table named EMPLOYEES2,

Secondly, the query will make the unique groups of the dept column, as there are three unique departments (SALES, MANUFACTURING, and ACCOUNTING)

And results in the output with a maximum salary of all three individual groups.

Using GROUP BY clause with AVG AGGREGATE function




As we have learned earlier that AVG aggregate function is used to find the average of numerical values from a specified column

For this, we will consider same above table named EMPLOYEES2 for practices.

Example



Description




The above query will also select the two columns named DEPT and AVG_SALARY (aliased) from table,

Note here we have used WHERE condition and IN operator, to read about SQL operator click here

Now the where condition will limit the search operation and search values for only two departments that are sales and accounting,

Using GROUP BY clause with COUNT AGGREGATE function

Right previous examples, you can operate it similarly, just as you better know that count aggregate function is used to count the total records of a column or entire table

Example



Description

Above example shows the total number of records per group (dept)

Similarly, you can use all other Aggregate functions with Group by clause.

Comments

Popular posts from this blog

How to create, use and manage RMAN STORED SCRIPTS in Oracle Database?

RMAN Stored Scripts helps to reduce the need for writing the repetitive commands for making the backups. Types of RMAN Stored Scripts- There are two types of Stored Scripts Local Scripts Global Scripts Local Scripts are available only for the target database, whereas Global scripts are those scripts which are available which are accessible to all the target databases sharing the Recovery catalog. Read more about: Recovery catalog in Oracle database. Configure your database for Recoverability.   Creating the RMAN Stored Scripts- Before creating Scripts, you are required to be connected to Target database and Recovery catalog database. Syntax: RMAN>create script script_name { backup_commands; } RMAN> create global script global_script_name { backup_commands; }. Examples: RMAN>create script db_backup { backup database plus archivelog; } In above example, we have created a local script that upon execution will create a full backup of database including all the archived r...

How to manage the Oracle Roles?

A ROLE is an oracle tool that simplifies the distribution of multiple privileges granting to the right users,rather than granting individual privileges to the user. First you will create a Role,then assign some privileges to that role, and then assign that role to the right user so all the privileges assigned to role will be granted to user automatically.   read also how to manage the oracle users? , how to grant and revoke the oracle system privileges? how to create the oracle indexes? how to create and use the oracle views?   How to create the Role Oracle roles can be created with following types of authentications NOT IDENTIFIED authentication PASSWORD authentication EXTERNAL authentication GLOBAL authentication If you skip specifying the authentication clause, Oracle by default create role with no authentication (NOT IDENTIFIED clause) Syntax CREATE ROLE role_name IDENTIFIED BY password; Examples Creating Role with the NOT IDENTIFIED clause. Role is authorized by da...

Modification of tablespaces and datafiles in oracle,using alter tablespace statement.

In the following tutorial,we will learn the modification of tablespaces and corresponding datafiles using alter tablespace and alter database statements, which includes the following Read more about What is an oracle tablespace and how to create it? Increasing and decreasing the size of tablespaces (resizing tablespace) Adding a datafile to a tablespace Taking a tablespace offline or online Making tablespace read only and read write Putting tablespace in backup mode Creation of tablespace with oracle managed file feature Renaming Tablespaces Relocating a tablespace Coalescing tablespaces Increasing and decreasing the size of tablespaces (resizing tablespace) The size of a tablespace can be increase by following three individual methods Resizing the existing tablespace Adding new datafiles Autoextend feature of datafile Resizing the existing datafile You can resize the existing datafile by using resize keyword in alter database  command as follow SQ...