EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Oracle Aggregate Functions

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » Oracle Tutorial » Oracle Aggregate Functions

Oracle Aggregate Functions

Introduction to Oracle Aggregate Functions

Oracle Aggregate function is a type of function which operates on specified column and returns a single row result. These functions are mostly used in a query which contains GROUP BY clause in a SELECT statement where GROUP BY clause groups the rows as per specified condition and AGGREGATE function performs aggregation on the grouped data.

  • It returns a single row result based on groups of rows conditions.
  • Aggregate functions can be used in SELECT statement.
  • Aggregate functions can be used in HAVING clause as a condition.
  • It returns one result per group as an output.
  • The group of rows can be the whole table or the rows split into many groups.

Important Aggregate Functions:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Functions Description
AVG ( ) It returns an average value of given expression.
SUM ( ) It returns the sum value of given expression.
MAX ( ) It returns maximum value of given expression.
MIN ( ) It returns minimum value of given expression.
COUNT ( ) It returns total no of rows for the given expression.
STDDEV ( ) It returns the standard deviation of the column.
VARIANCE ( ) It returns the variance.

Syntax of Oracle Aggregate Functions

Given below is the syntax :

GroupFunctionName (DISTINCT / ALL ColumnName)

Guidelines to Use:

  • DISTINCT keyword is used to ignore duplicate values and function consider only non-duplicate values.
  • ALL keyword is used to consider all values for the aggregate operation including duplicates.
  • CHAR, VARCHAR2, NUMBER or DATE data types can be used for argument.
  • All group function except COUNT (*) function ignore NULLs. The NVL function can be a better option to substitute a value for NULLs.
  • No single row column(s) should be used with group function in SELECT statement without GROUP BY clause.

Examples of Oracle Aggregate Functions

Given below are the examples mentioned:

We will use the below sample table (Employee) with 14 records to see the Oracle Aggregate function behavior.

Code:

SELECT * FROM Employee;

Output:

Oracle Aggregate Functions 1

Example #1

AVG (DISTINCT / ALL ColumnName) Function.

  • Output will be the Average value of column.
  • It doesn’t consider NULL values.

Code:

SELECT AVG(Salary), AVG(DISTINCT Salary) FROM Employee;

Output:

Oracle Aggregate Functions 2

In the above example, there are two average values of the salary column and both are different because first average function operates on all values of the column but the second average function operates on unique values because of DISTINCT keyword. That’s why both results are different.

Code:

SELECT AVG(Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 3

In the above example, there is an average value of the Bonus column and bonus column contains NULL values. The AVG function ignores NULL values and calculate the average of the not null values.

Example #2

SUM (DISTINCT / ALL ColumnName) Function.

  • Output will be the SUM value of column.
  • It doesn’t consider NULL values.

Code:

SELECT SUM (Salary), SUM (DISTINCT Salary) FROM Employee;

Output:

Oracle Aggregate Functions 4

In the above example, there are two sum values of the salary column and both are different because first sum function operates on all values of the column but the second sum function operates on unique values because of DISTINCT keyword. That’s why both results are different.

Code:

SELECT SUM (Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 5

In the above example, there is one sum value of the Bonus column and bonus column contains NULL values. The SUM function ignores NULL values and calculate the sum of not null values.

Example #3

MAX (DISTINCT / ALL ColumnName) Function.

  • Output will be the maximum value of column.
  • It doesn’t consider NULL values.

Code:

SELECT MAX(Salary), MAX(DISTINCT Salary) FROM Employee;

Output:

max

In the above example, there are two maximum values of the salary column and both are same but first maximum function operates on all values of the column but the second maximum function operates on unique values because of DISTINCT keyword and returns an appropriate output.

Popular Course in this category
Sale
Oracle Training (14 Courses, 8+ Projects)14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,901 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst Bundle - 250+ Courses, 40+ Projects

Code:

SELECT MAX(Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 7

In the above example, there is a maximum value of the Bonus column and bonus column contains NULL values. The MAX function ignores NULL values and returns the maximum value of that column.

Example #4

MIN (DISTINCT / ALL ColumnName) Function.

  • Output will be the minimum value of column.
  • It doesn’t consider NULL values.

Code:

SELECT MIN (Salary), MIN (DISTINCT Salary) FROM Employee;

Output:

min

In the above example, there are two minimum values of the salary column and both are same but first minimum function operates on all values of the column but the second minimum function operates on unique values because of DISTINCT keyword and returns an appropriate output.

Code:

SELECT MIN (Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 9

In the above example, there is a minimum value of the Bonus column and bonus column contains NULL values. The MIN function ignores NULL values and returns the minimum value of that column.

Example #5

STDDEV (DISTINCT / ALL ColumnName) Function.

  • Output will be the Standard Deviation of column.
  • It doesn’t consider NULL values.

Code:

SELECT STDDEV(Salary), STDDEV(DISTINCT Salary) FROM Employee;

Output:

STDDEV

In the above example, there are two Standard Deviation values of the salary column and both are different because first Standard Deviation function operates on all values of the column but the second Standard Deviation function operates on unique values because of DISTINCT keyword. That’s why both results are different.

Code:

SELECT STDDEV(Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 11

In the above example, there is a Standard Deviation value of the Bonus column and bonus column contains NULL values. The STDDEV function ignores NULL values and calculate the Standard Deviation of the not null values.

Example #6

VARIANCE (DISTINCT / ALL ColumnName) Function.

  • Output will be the Variance of N.
  • It doesn’t consider NULL values.

Code:

SELECT VARIANCE(Salary), VARIANCE(DISTINCT Salary) FROM Employee;

Output:

variance

In the above example, there are two Variance values of the salary column and both are different because first variance function operates on all values of the column but the second variance function operates on unique values because of DISTINCT keyword. That’s why both results are different.

Code:

SELECT VARIANCE (Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 13

In the above example, there is a variance value of the Bonus column and bonus column contains NULL values. The VARIANCE function ignores NULL values and calculate the variance of the not null values.

Example #7

COUNT (*/ DISTINCT / ALL ColumnName) Function.

  • Output will be the number of rows.
  • With COUNT function, * is used to return all rows including duplicates and NULLs.
  • It is used to get the count of all rows or distinct values of column.

Code:

SELECT COUNT(*) FROM Employee;

Output:

count

In the above example, COUNT function returns 14 because * considers all rows including duplicates and NULLs.

Code:

SELECT COUNT(Designation), COUNT(DISTINCT Designation) FROM Employee;

Output:

count

In the above example, returns two count values of the Designation column and both are different because first count function operates on all values of the column but the second count function operates on unique values because of DISTINCT keyword and returns an appropriate output.

Code:

SELECT COUNT(Bonus) FROM Employee;

Output:

Oracle Aggregate Functions 17

In the above example, there is a count value of the Bonus column and bonus column contains NULL values. The COUNT function ignores NULL values and count only not null values.

Example #8

GROUP BY Clause with Aggregate Function.

  • GROUP BY clause is used for grouping the column.
  • If any aggregate function is included in a SELECT statement with non-group functional columns then it is used for grouping the non-group column(s).

Code:

SELECT Deptnumber, MAX (Salary) FROM Employee;

Output:

GROUP BY

The above example, returns error because there is a non-group functional column is used with an aggregate function MAX. So the SELECT statement returns all rows for Deptnumber column but aggregate function returns only one row. To avoid the error GROUP BY clause can be used.

Code:

SELECT Deptnumber, MAX (Salary) FROM Employee GROUP BY Deptnumber;

Output:

Oracle Aggregate Functions 19

Tip: These functions can be used on any data type.

Conclusion

Oracle Aggregate Functions are very useful to calculate on a group of rows and return a single value for each group. It allows us to summarize data from multiple rows of a table or view.

Recommended Articles

This is a guide to Oracle Aggregate Functions. Here we discuss the introduction to Oracle Aggregate Functions along with examples for better understanding. You may also have a look at the following articles to learn more –

  1. Oracle LOCK TABLE
  2. IF THEN ELSE in Oracle
  3. Oracle REGEXP_REPLACE
  4. Oracle Database Administration

Oracle Training (14 Courses, 8+ Projects)

14 Online Courses

8 Hands-on Projects

120+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - Oracle Training (14 Courses, 8+ Projects) Learn More