EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Aggregate Functions

Oracle Aggregate Functions

Priya Pedamkar
Article byPriya Pedamkar

Updated May 15, 2023

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.

ADVERTISEMENT
Popular Course in this category
ORACLE DBA Database Management System Course Bundle - 2 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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:

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.

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
ADVERTISEMENT
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Let’s Get Started

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

*Please provide your correct email id. Login details for this Free course will be emailed to you
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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW