Aggregating Data using Group Functions (2024)

Overview

Using Group Functions

Using Group-By Clause

Using Having-Clause

Nesting Group Functions

Overview

Group functions are mathematical functions to operate on sets of rows to giveone result per set. The types of group functions (also called aggregatefunctions) are:

  • AVG, that calculates the average of the specified columns in a set of rows,
  • COUNT, calculating the number of rows in a set.
  • MAX, calculating the maximum,
  • MIN, calculating the minimum,
  • STDDEV, calculating the standard deviation,
  • SUM, calculating the sum,
  • VARIANCE, calculating the variance.

The general syntax for using Group functions is :

SELECT <column, ...>, group_function(column)

FROM <table>

WHERE <condition>

[GROUP BY <column>]

[ORDER BY <column>]

Note that the column on which the group function is applied must exist in theSELECT column list.

For example, the following selects the maximum salary from the employeetable.

SQL> SELECT dept, MAX(sal)

FROM employee;

Using Group Functions

You can use AVG and SUM for numeric data. MIN and MAX can be applied on anydata type including numbers, dates and strings. The order is definedaccordingly.

Group functions ignore the NULL values in the column. To enforce the groupfunctions ti include the NULL value, use NVL function.

For example, the following statement forces to take the average for allsalary values even including 0 (although it is an odd case, let's assume this ispossible.)

SQL> SELECT dept, AVG(NVL(sal,0))

FROM employee;

Using Group-By Clause

Note that ALL columns in the SELECT list that are not in group functions mustbe in the GROUP-By clause.

For example,

SQL> SELECT dept, job, MAX(sal)

FROM employee

GROUP BY dept, job;

The following statement is thus illegal, because column dept is not in anaggregate function and is not in GROUP-BY clause.

SQL> SELECT dept, MAX(sal)

FROM employee;

The correct statement which selects the maximum salary for each department,should be:

SQL> SELECT dept, MAX(sal)

FROM employee

GROUP BY dept;

Using Having-Clause

To exclude some group results, use Having-clause.

For example,

SQL> SELECT dept, MAX(sal)

FROM employee

GROUP BY dept

HAVING MAX(sal) > 2000;

Note that you CANNOT use WHERE clause to restrict groups. For example,the following is illegal:

SQL> SELECT dept, MAX(sal)

FROM employee

WHERE MAX(sal) > 2000

GROUP BY dept;

This is because the group function cannot be used in WHERE clause.

Nesting Group Functions

You can nest group function calls. For example, the following example to showthe maximum of the average salary of all departments,

SQL> SELECT MAX(AVE(sal))

FROM emp

GROUP BY dept_no;

Aggregating Data using Group Functions (2024)
Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6175

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.